An instance of an object .. programming basics question.

So I am actually really close ( within a week) of publishing my first game, programming everything myself. Yet I am totally confused as to why I get this error. I have always found work-arounds to avoid the error (mainly making everything static) but I want to know how to do this right. I have a public class with a public variable, I cannot make the variable static because I need to assign it in the editor.

public class Solar : MonoBehaviour
public GameObject solarshield;

Now I want to be able to enable solarshield from a different script. This shouldn´t be hard, right? This is what I have in the other script. I get the error ´object reference not set to an instance of an object

Solar solar = new Solar ();
solar.solarshield.SetActive (true);

I also tried

GameObject go = new Solar.solarshield ();
go.SetActive (true);

expecting ( error

If someone could show me what I need to change and maybe explain what is wrong. Thanks!

The problem is that instead of finding the actual GameObject in the scene, which has this solar script attached to it, and activating the solarshield on that GameObject, you are creating a new instance of the Solar class (which probably does not have anything assigned to the solarshield variable) and activating that - this gives a NullReferenceException.

This is a very common misconception. You have to realize that the Solar class is just a code. If you want to use it in your game, you have to create an instance of it. You are creating this instance by adding it as a component to a GameObject.

When you say, you want to access another script, you actually want to access the instance - which is on that specific GameObject. For example, if you have two GameObjects with the Solar script attached to them, you will have an instance 01, and an instance 02 of that script. If you want to change the first instance, obviously you can not access the second one - that makes no sense. That’s why you need references.

Instead of creating a new instance (which is basically a blank Solar script), you should search for the GameObject (which has the Solar script on it, that you want to change), get the Solar script component, and after that you can access that variables of that instance.

For example, if you have a GameObject in the scene (lets name it ‘ObjectA’), you can use this line to access the variable from another script:

GameObject.Find("ObjectA").GetComponent<Solar>().solarshield

By this, you can use the solarshield GameObject reference on the Solar script component, which is attached to ObjectA.

If you can understand this, you will have no problem with accessing other instances later. And be careful about static variables, since they will be shared across all instances - which makes this whole concept no longer useful. With static variables you will not be able to change instances separately, which is crucial in order to create diverse objects.

Thank you. that makes perfect sense. So my problem is I was thinking of the Solar script as the top object and ignoring the fact that the Solar script I wanted is already attached to an object and I should be starting there. I´ve run into this error a couple of other times, and always they tell me ´just make it static´ and because the game is small, that is easy enough to do and works well, but in the back of my head I know it isn´t the best way to do it.

thank you, that is an immense ´learn´ for me today.