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

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #16395
    Isaac
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    At

    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;
    }

    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 🧐

    #16396
    Isaac
    Level 2
    Participant
    Helpful?
    Up
    0
    ::
    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;
    }
    #16402
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share your NullReferenceException error message?

    #16411
    Isaac
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    NullReferenceException: Object reference not set to an instance of an object.
    Projectile.GetSpawnAngle

    #16417
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share the entire piece of code that contains your NullReferenceException?

    #16421
    Isaac
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    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

    protected virtual float GetSpawnAngle() //Get the direction the projectile should face when spawning
    {
    return Mathf.Atan2(movement.lastMovedVector.y, movement.lastMovedVector.x) * Mathf.Rad2Deg;
    }
    #16422
    Isaac
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    I have checked and added the new code.

    At ProjectileWeapon.cs I add this new line into the script

    public override void Start()
        {
    
        if (movement == null)
        {
            Debug.LogWarning("Movement is not assigned or cannot be found.");
        }
    }</code></pre>

    At GetSpwanAngle() I added this debug to check player movement by using this line

    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></pre>

    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.

    #16423
    Isaac
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    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?

    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;
        }
    
    }

    }

    #16424
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    The issue is very likely that the movement variable is empty in ProjectileWeapon. That is what is causing the NullReferenceException.

    How is your movement variable set in ProjectileWeapon? Are you still using the PlayerMovement component on ProjectileWeapon?

    You can check out this 1-minute video to understand null reference exceptions better as well.

    #16488
    Isaac
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    I have fixed it. Ty guy <3

    #16490
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Fantastic. Do you mind sharing more about what was the issue?

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: