Finding a Script Attached to a Game Object

I’ve completed the Space Shooter tutorial and have been trying to add something I thought would be simple. I’m trying to make it so that when the player hits a button, they will initiate a “dash” where the player’s speed increases and they become invulnerable for about a quarter second. My problem is that for some reason I’m making it so that neither the asteroids nor the player’s ship is ever destroyed, regardless of whether they’re dashing or not. I’m getting NullReferenceException: Object Reference not set to an instance of an object and I’m not sure why.

The idea was to create a variable to hold an instance of the playerController script that is attached to the player’s ship so that I would be able to access its variables, just like the tutorial did with the GameController object. Unfortunately PlayerController is a script attached to the Player object, it isn’t an object itself that I can just drag and drop in the Hierarchy.

Here’s the code in the DestroyByContact script.

public class DestroyByContact : MonoBehaviour {
    public GameObject explosion;
    public GameObject playerExplosion;
    public int scoreValue;
    private GameController gameController;
    private PlayerController playerController;
   
    void Start()
    {
        GameObject gameControllerObject = GameObject.FindGameObjectWithTag("GameController");
        //Adding the following two lines causes the errors.
        GameObject playerControllerObject = GameObject.Find("PlayerController");
        playerController = playerControllerObject.GetComponent<PlayerController>();

        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameController == null)
        {
            Debug.Log("Cannot find 'GameController' script");
        }     
    }

    void OnTriggerEnter (Collider other)
    {
       
        if (other.tag == "Boundary")
        {
            return;
        }

        Instantiate(explosion, transform.position, transform.rotation);

        if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gameController.GameOver();

           //Lines 42-46 are commented out to test my code. It changes nothing when commented out, so it's not the problem.
            
         /* if (playerController.invincible == false)
            {
                Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
                gameController.GameOver();               
            }*/
        }
            //The next 3 lines no longer work after adding in lines 12 and 13.
            gameController.AddScore(scoreValue);
            Destroy(other.gameObject);
            Destroy(gameObject); 
    }

}

Double click the null reference error in the console. What line of that code does it take you to? It could be a number of things.

By the way, you can drag MonoBehaviours into global variables of other scripts:

public PlayerController controller;

Then just drag the object the PlayerController is attached to into the inspector, and it’ll find the script attached to it.

1 Like

Hi Scabbage, thanks for the reply.

I get two NullReference errors. One takes me to line 38: gameController.GameOver();
The other takes me to line 13: playerController = playerControllerObject.GetComponent();

Note that the error on line 38 does not occur if I remove lines 12 and 13.

I can’t seem to get that to work. It won’t let me drag the “Player” object onto the playerController variable in the editor, nor will it let me drag the PlayerController script.

That’s assuming the Player object has a PlayerController on it and the playerController variable is a PlayerController object. The original script also has the playerController variable private…

The first error is because your gameController variable isn’t assigned (ie. a null reference). The second could either be because the playerControllerObject is null or the playerControllerObject doesn’t have a PlayerController script attached.