Bullet Bug

I’m trying to make it so the player can’t shoot while on the ladder. When I press the shoot button it’s telling me that “NullReferenceException: Object reference not set to an instance of an object”, not sure why. Error is on line 55 aka the second line in the OnFire method

Player script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    Vector2 moveInput;
    Rigidbody2D rb2D;
    Animator animator;
    public CapsuleCollider2D capsuleCollider2D;
    BoxCollider2D boxCollider2D;
    
   BulletScript bulletScript;
    
    float gravityStartScale;
    bool isAlive = true;

    [SerializeField] float moveSpeed = 5f;
    [SerializeField] float jumpHeight = 5f;
    [SerializeField] float climbSpeed = 5f;
    [SerializeField] float climbSpeedDOWN = -3f;
    [SerializeField] Vector2 deadKick = new Vector2 (15f,0f);
    [SerializeField] GameObject Bullet;
    [SerializeField] Transform BulletSpawn;

    void Awake()
    {
        rb2D = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        capsuleCollider2D = GetComponent<CapsuleCollider2D>();
        boxCollider2D = GetComponent<BoxCollider2D>();
        bulletScript = FindObjectOfType<BulletScript>();

        
       

        gravityStartScale = rb2D.gravityScale;
    }


    void Update()
    {
        if (!isAlive) {return;}
        Run();
        FlipSprite();
        NotClimbingLadder();
        Die();
        
    }

    void OnFire(InputValue value)
    {
        if (!isAlive) {return;}
        if (bulletScript.isOnLadder) {return;}
        Instantiate(Bullet, BulletSpawn.position, transform.rotation);
    }

    void OnMove(InputValue value)
    {
        if (!isAlive) {return;}
        moveInput = value.Get<Vector2>();
        
    }

     void OnJump(InputValue value)
    {
        if (!isAlive) {return;}

        if(!boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
           
            
            return;
            
        }

        

         if(rb2D.gravityScale == 0 && boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            
            rb2D.gravityScale = gravityStartScale;
            animator.SetBool("IsClimbing", false);

        }
        

        
        if(boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
           
            rb2D.gravityScale = gravityStartScale;
            
            if (value.isPressed)
            {
                rb2D.velocity += new Vector2 (0f, jumpHeight);
            }
            
        }



        
    }

    void Run()
    {
        Vector2 playerVelocity = new Vector2(moveInput.x * moveSpeed, rb2D.velocity.y);
        rb2D.velocity = playerVelocity;

        if (Mathf.Abs(rb2D.velocity.x) > Mathf.Epsilon)
        {
            animator.SetBool("IsRunning", true);
        }

        else
        {
            animator.SetBool("IsRunning", false);
        }
        
    }

    void FlipSprite()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(rb2D.velocity.x) > Mathf.Epsilon;

        if (playerHasHorizontalSpeed)
        {
        transform.localScale = new Vector2 (Mathf.Sign(rb2D.velocity.x), 1f);
        }
        
    }
        
    void NotClimbingLadder()
    {

       

         if(!boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
        {
            rb2D.gravityScale = gravityStartScale;
            animator.SetBool("IsClimbing", false);
            //Debug.Log("NotTouchingLadder");
            return;
        }

        if(Mathf.Abs(rb2D.velocity.y) == 0 && boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")) && capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
        {
            rb2D.gravityScale = gravityStartScale;
            animator.SetBool("IsClimbing", false);
    
           
        }

            
    }


    
    void OnClimbingUP(InputValue value)
        {
            if(boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
            {
            
            Vector2 climbVelocity = new Vector2(rb2D.velocity.x, moveInput.y * climbSpeed);
            rb2D.velocity = climbVelocity;
            rb2D.gravityScale = 0f;

            bool playerHasVerticalSpeed = Mathf.Abs(rb2D.velocity.y) > Mathf.Epsilon;
            animator.SetBool("IsClimbing", playerHasVerticalSpeed);

            Debug.Log("ClimbingUP");
            }
        }

    void OnClimbingDOWN(InputValue value)
    {
        
        
        if(boxCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
            {
            
            Vector2 climbVelocityDOWN = new Vector2(rb2D.velocity.x, moveInput.y * climbSpeedDOWN * -1);
            rb2D.velocity = climbVelocityDOWN;
            rb2D.gravityScale = 0f;

            bool playerHasVerticalSpeed = Mathf.Abs(rb2D.velocity.y) > Mathf.Epsilon;
            animator.SetBool("IsClimbing", playerHasVerticalSpeed);
             Debug.Log("ClimbingDOWN");
            }
        
    }

     void Die() 
    {
        if (capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Enemies", "Hazards")))
        {
            isAlive = false;
            animator.SetTrigger("Dying");
            rb2D.velocity = deadKick;
        }
    }
        
    


}

Bullet Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletScript : MonoBehaviour
{
   [SerializeField] float bulletSpeed = 5f;
   float xSpeed;
   public bool isOnLadder = true;

    Rigidbody2D rb2D;

    PlayerMovement player;


    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
        player = FindObjectOfType<PlayerMovement>();
        xSpeed = player.transform.localScale.x * bulletSpeed;
    }

    
    void Update()
    {
        
        //OnLadder();
        Shoot();
        
    }

   void OnTriggerEnter2D(Collider2D other) 
    {
        if(other.tag == "Enemy")
        {
            Destroy(other.gameObject);
        }

        Destroy(gameObject);
    }

   void OnCollisionEnter2D(Collision2D other)
    {
        Destroy(gameObject);
    }

    void Shoot()
    {
        if(isOnLadder) {return;}
        rb2D.velocity = new Vector2(xSpeed, 0f);
    }

    void OnLadder()
    {
        if(player.capsuleCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
        {
            isOnLadder = true;
            Debug.Log("Hello");
        }
    }
    
}

I notice your isOnLadder is defaulted to True, so it’s always saying is on ladder?
And is never reset to false?

ah oops changed it to false but I still get the error

Is the bullet script assigned as a component?

yes it’s assigned as a component directly on the prefab

Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Privacy & Terms