Need help an error

For some reason, an error (stated on the video), pops up whenever my player shoots a bullet. I observed my code and I couldn’t seem to find anything wrong although this is my first time using this code so I’m probably overlooking something here.
I’d also like to note that this error doesn’t seem to affect the game in any way except for filling the console with said error although I might be missing something.

The video below depicts when the error pops up and the code that is related to the error.

Also, I’m gonna include my Player Script into the thread as well in case I’m missing something there as well.
Warning: You are about to see very and I mean VERY messy code.

[

using UnityEngine;
using UnityEngine.UI;

public class PlayerHandler : MonoBehaviour {

    #region Player Variables
    public GameObject Player1;
    public float P1health;
    public GameObject Particles1;
    public GameObject Emitter1;
    public GameObject Player2;
    public float P2health;
    public GameObject Particles2;
    public GameObject Emitter2;
    private Rigidbody rb1;
    private Rigidbody rb2;
    private bool isFiring1;
    private bool isFiring2;
    private bool grounded;
    private bool grounded2;
    private int BulletDirection1;
    private int BulletDirection2;
    #endregion

    #region Shared Variables
    public float speed;
    public float JumpHeight;
    #endregion

    #region Others
    public GameObject DeathParticles;
    public GameObject WinTextRoot;
    public Text WinText;
    public Text HealthDisplay;
    public GameObject Bullet;
    public float bulletSpeed;
    private bool Death;

    #endregion

    void Fire(){
        if(isFiring1 == true){
            GameObject Temporary_Bullet_Handler;
            Temporary_Bullet_Handler = Instantiate(Bullet,Emitter1.transform.position,Emitter1.transform.rotation) as GameObject;

            if(BulletDirection1 == 1){
            Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0, 0, 90);
            }

            if(BulletDirection1 == -1){
                Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0,0,270);
            }

            Rigidbody Temporary_RigidBody;
            Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();

            Temporary_RigidBody.AddForce(new Vector3(BulletDirection1, 0, 0) * bulletSpeed);

            Destroy(Temporary_Bullet_Handler, 3.0f);
            }
            if(isFiring2 == true){
            GameObject Temporary_Bullet_Handler;
            Temporary_Bullet_Handler = Instantiate(Bullet,Emitter2.transform.position,Emitter2.transform.localRotation) as GameObject;
          
            if(BulletDirection2 == 1){
            Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0, 0, 90);
            }

            if(BulletDirection2 == -1){
                Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0,0,270);
            }

            Rigidbody Temporary_RigidBody;
            Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();

            Temporary_RigidBody.AddForce(new Vector3(BulletDirection2, 0, 0) * bulletSpeed);

            Destroy(Temporary_Bullet_Handler, 3.0f);
            }

    }

    void GameOver(){
        if(P1health <= 0){
        Destroy(Player1);  
        Instantiate(DeathParticles, Player1.transform.position,DeathParticles.transform.rotation);
        WinTextRoot.SetActive(true);
        WinText.text = "Player 2 has WON!";
        }
        if(P2health <= 0){
        Destroy(Player2);
        Instantiate(DeathParticles, Player2.transform.position, DeathParticles.transform.rotation);
        WinTextRoot.SetActive(true);
        WinText.text = "Player 1 has WON!";
        }
    }

    void Start () {
        rb1 = Player1.GetComponent<Rigidbody>();
        rb2 = Player2.GetComponent<Rigidbody>();
        BulletDirection1 = 1;
        BulletDirection2 = -1;
        isFiring1 = false;
        isFiring2 = false;
        Death = false;
    }
  
    void FixedUpdate () {
      
        if(rb1 != null){
        #region Player1Controls
        if(rb1.velocity.y <= -0.1 || rb1.velocity.y >= 0.1){
            grounded = false;
        }
        if(rb1.velocity.y == 0){
            grounded = true;
        }
      
        if(rb1.velocity.x <= -0.2 || rb1.velocity.x >= 0.2){
            Particles1.SetActive(true);
        }
        if(rb1.velocity.x == 0){
            Particles1.SetActive(false);
        }

        //move right
        if(Input.GetKey("d")){
            rb1.AddForce(Vector3.right * speed * Time.fixedDeltaTime);
            Player1.transform.rotation = Quaternion.Euler(0, 0, 0);
            BulletDirection1 = 1;
        }
        //move left
        if(Input.GetKey("a")){

            rb1.AddForce(Vector3.left * speed * Time.fixedDeltaTime);
            Player1.transform.rotation = Quaternion.Euler(0,180, 0);
            BulletDirection1 = -1;
        }
        //move up
        if(Input.GetKeyDown("w") && grounded == true){
            rb1.AddForce(Vector3.up * JumpHeight * Time.fixedDeltaTime);
        }
        if(Input.GetKeyDown(KeyCode.Space)){
            isFiring1 =true;
            Fire();
        }
        else{
            isFiring1 =false;
        }
        }
        #endregion

        #region PLayer2Controls
        if(rb2 != null){
            if(rb2.velocity.y <= -0.1 || rb2.velocity.y >= 0.1){
            grounded2 = false;
        }
        if(rb2.velocity.y == 0){
            grounded2 = true;
        }
            if(rb2.velocity.x <= -0.2 || rb1.velocity.x >= 0.2){
            Particles2.SetActive(true);
        }
        if(rb2.velocity.x == 0){
            Particles2.SetActive(false);
        }

        //move right
        if(Input.GetKey(KeyCode.RightArrow)){
            rb2.AddForce(Vector3.right * speed * Time.fixedDeltaTime);
            Player2.transform.rotation = Quaternion.Euler(0, 0, 0);
            BulletDirection2 = 1;
        }
        //move left
        if(Input.GetKey(KeyCode.LeftArrow)){

            rb2.AddForce(Vector3.left * speed * Time.fixedDeltaTime);
            Player2.transform.rotation = Quaternion.Euler(0,180, 0);
            BulletDirection2 = -1;
        }
        //move up
        if(Input.GetKeyDown(KeyCode.UpArrow) && grounded2 == true){
            rb2.AddForce(Vector3.up * JumpHeight * Time.fixedDeltaTime);
        }
        if(Input.GetKeyDown(KeyCode.RightControl)){
            isFiring2 =true;
            Fire();
        }
        else{
            isFiring2 =false;
        }
        }
        #endregion

        HealthDisplay.text = "Player1: " + P1health + "    Player2: " + P2health;

        if(P1health <= 0 && Death == false || P2health <=0 && Death == false){
            GameOver();
            WinTextRoot.SetActive(true);
            Death = true;
        }


    }
}

/SPOILER]

I think…

The player 1 bullet is hitting player 1 as soon as the bullet is created. It should kill player one but I think it might be because start has not run at this point so Players=Null…

Try changing Start to Awake and see if it then kill player 1. If so you then need to move the bullet starting point outside of the players collider or update the logic so the players cant take damage from their own bullets.

You are creating the bullet within fixedUpdate which may also be contributing to the problem.
Have a read up on fixedUpdate and Update.

As the console tells you, the error is happening inside your Bullet script on line 13.

Players.GetComponent<PlayerHandler>().P1health -= 10;

Null reference exception means you’re trying to access a property on a null value. In this case, it means that either Players is null at that point, or GetComponent isn’t finding a PlayerHandler component on the Players object. To figure out which it is, you can split that line up into 2 and see which one throws the error:

PlayerHandler ph = Players.GetComponent<PlayerHandler>();
ph.P1health -= 10;

Debugging is all about tracing the path back to the source of the problem. Find a spot where it’s breaking, and trace it backwards until you spot the root.

1 Like

Thank you very much for both of the advice I have been given. I will try out your suggestions tomorrow when I have the time because as of the time of writing this it’s nighttime at my place.