Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.
Forum begins after the advertisement:
[Part 15] NullReferenceException: Object reference not set to an instance of an
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 15] NullReferenceException: Object reference not set to an instance of an
- This topic has 10 replies, 2 voices, and was last updated 11 months, 1 week ago by
Terence.
-
AuthorPosts
-
November 18, 2024 at 6:44 pm #16395::
At
<code>protected virtual float GetSpawnAngle() //Get the direction of the projectile should face when spawning { return Mathf.Atan2(movement.lastMovedVector.y, movement.lastMovedVector.x) * Mathf.Rad2Deg; }</code>I found the same topic and I already checked the owner by using Debug.Log($”Owner: {owner.name}”); at Weapon:Item It shows ” owner: Player ” and I already assigned the weaponPrefab as a projectile into a WeaponData. Inside the weaponPrefab, I have added Projectile.cs too. What did I miss 🧐
November 18, 2024 at 6:46 pm #16396::<code>protected virtual float GetSpawnAngle() //Get the direction of the projectile should face when spawning { return Mathf.Atan2(movement.lastMovedVector.y, movement.lastMovedVector.x) * Mathf.Rad2Deg; }</code>November 18, 2024 at 7:24 pm #16402November 18, 2024 at 9:20 pm #16411::NullReferenceException: Object reference not set to an instance of an object. Projectile.GetSpawnAngle
November 19, 2024 at 1:47 pm #16417November 19, 2024 at 4:42 pm #16421::Here from this line. When the game starts there is no weaponPrefab spawn. I don’t know why it happens I have added the weaponPrefab in the weaponData
<code>protected virtual float GetSpawnAngle() //Get the direction the projectile should face when spawning { return Mathf.Atan2(movement.lastMovedVector.y, movement.lastMovedVector.x) * Mathf.Rad2Deg; }</code>November 19, 2024 at 6:17 pm #16422::I have checked and added the new code.
At ProjectileWeapon.cs I add this new line into the script
<code>public override void Start() { if (movement == null) { Debug.LogWarning("Movement is not assigned or cannot be found."); } }</code>At GetSpwanAngle() I added this debug to check player movement by using this line
<code>protected virtual float GetSpawnAngle() //Get direction the projectile should face when spawning { if (movement == null) { Debug.LogWarning("Movement is null. Cannot calculate spawn angle."); return 0f; } return Mathf.Atan2(movement.lastMovedVector.y, movement.lastMovedVector.x) * Mathf.Rad2Deg; }</code>And yes. when I press start, it shows all the LogWarning I have added. It must be an issue with the PlayerController.cs for sure.
November 19, 2024 at 6:29 pm #16423::Here is my PlayerController.Cs I am using the NewInputSystem to control player movement. I think the problem lies in the NewInputSystem. Should I revert to using the original script?
<code>using UnityEngine; using UnityEngine.InputSystem; public class PlayerController : MonoBehaviour { public static PlayerController instance; [HideInInspector] public Vector2 moveDir; //Move Directory [HideInInspector] public Vector2 lastMovedVector; [HideInInspector] public float lastHorizontalVector; //Check last HorizontalVector [HideInInspector] public float lastVerticalVector; //Check last VerticalVector Animator anim; SpriteRenderer spriteRenderer; Rigidbody2D rb; PlayerStats playerStat; void Awake() { playerStat = GetComponent<PlayerStats>(); rb = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); spriteRenderer = GetComponent<SpriteRenderer>(); if (instance == null) { instance = this; } else { Debug.LogWarning("EXTRA" + this + "DELETE"); Destroy(gameObject); } lastMovedVector = new Vector2(1, 0f); //at start of game the projectile weapon will have momentum } void Update() { InputManagement(); if (moveDir.x != 0 || moveDir.y != 0) { anim.SetBool("Move", true); SpriteDirectionCheck(); } else { anim.SetBool("Move", false); } } void FixedUpdate() { rb.velocity = new Vector2(moveDir.x * playerStat.MoveSpeed, moveDir.y * playerStat.MoveSpeed); } private void InputManagement() { //moveDir = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).normalized; if (moveDir.x != 0) { lastHorizontalVector = moveDir.x; lastMovedVector = new Vector2(lastHorizontalVector, 0f); } if (moveDir.y != 0) { lastVerticalVector = moveDir.y; lastMovedVector = new Vector2(0f, lastVerticalVector); } if (moveDir.x == 0 && moveDir.y == 0) { lastMovedVector = new Vector2(lastHorizontalVector, lastVerticalVector); } } public void Move(InputAction.CallbackContext context) { moveDir = context.ReadValue<Vector2>().normalized; } void SpriteDirectionCheck() { // x < 0 = left // flip if (lastHorizontalVector < 0) { spriteRenderer.flipX = true; } else { spriteRenderer.flipX = false; } } }</code>November 19, 2024 at 7:44 pm #16424::The issue is very likely that the
movementvariable is empty inProjectileWeapon. That is what is causing theNullReferenceException.How is your
movementvariable set inProjectileWeapon? Are you still using thePlayerMovementcomponent onProjectileWeapon?You can check out this 1-minute video to understand null reference exceptions better as well.
November 22, 2024 at 10:03 pm #16488November 22, 2024 at 10:54 pm #16490 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below:
Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.