ERROR: NullReferenceException: Object reference not set to an instance of an object

Hi,

I have this shield gameobject and I have created a script for it to set its render to false during the start of the game so the gameobject is invisible.

here is the code for it:

public class Shield : MonoBehaviour
{
// Use this for initializationk
void Start ()
{

}

// Update is called once per frame
void Update ()
{
Visible(false);
}

public bool Visible (bool v) {
renderer.enabled = v;
return v;
}
}

And what I want it when two diffrernt gameobjects collide with each other this gameobject will appear so what I did was I called the Shield script onto another script which is called Powerup script like this:

public class Powerup : MonoBehaviour
{
public Shield shield;

On this script I have created an OnTriggerEnter function and everything there is working except for the last line of the code inside the OnTriggerEnter function here is the code:

public void OnTriggerEnter(Collider otherObject)
{
if(otherObject.tag == “Player”)
{
transform.position = new Vector3(0.0f,0.0f);
xAxis = Random.Range(-2,2);
yAxis = Random.Range(-2,2);
shield.Visible(true); ---------------------------------------> this one is not working with an error NullReferenceException: Object reference not set to an instance of an object
}
}

Can anyone please help me what does this mean? I know this might be an easy problem to be solved but I am just new at programming an I have searched all over the internet for a solution I cannot find one.

You have to initialize your reference variables before you can use them. Your shield variable is just a reference to another object and if you don’t initialize them correctly the reference points to nothing. Thats the reason for the error message. Either you have to create such objects with the new statement or you can let them point to an object which is created somewehre else. Because you’re using here two classes which are derived from MonoBehaviour Unity finishs the creation of the objects for you. You just have to get it and initialize your reference with it. You can do this either in the Inspector in Unity by drag and drop the game object into the right fields or you can do this with the GetComponent method of the GameObject in a script. But enough about the theory :wink:

If you want to do this in the Inspector:

Drag your Shield game object from the hierachy view into the Shield field of the Powerup gameobject in the Inspector view.

Or if you you want to do it in a script:

I’m assuming you have put the two components (Shield and Powerup) on the same gameobject in the Inspector than you can get the reference to the Shield component easily in your Awake method of the Powerup component

public class Powerup : MonoBehaviour
{
    public Shield shield;

    void Awake()
    {
        shield = GetComponent<Shield>();
    }
}

You can use the GetComponent method every time you want but its good practice to do it just once in the Awake or Start method to get a better performance

Hi THoeppner,

Thanks for the theory it was really a great read and awesome feedback from you, I liked your theory it taught me alot particularly on how to use the GameComponent. This will serve a good reviewer for me in the near future just in case I forget how to properly use the GameComponent and how properly initialize my reference variables :smile:. I used what you told me and you instructions were really clear I really understood it well. Thanks again for the reply and helping me out on this one. Awesome! :slight_smile: