NullReferenceException while trying to access script on object

Hello,

I’ve a script attached to a gameobject, this script called “testvar.cs” and contains:

public int great = 123;

Now I need to change “great” value from 123 to 2, from another script, and I’m using this line:

 playerPrefab.GetComponent<testvar>().great = 2;

playerPrefab is a the public gameobject (set with the inspector)where I’ve “testvar.cs” script.
By the way that line of code always produce NullReferenceException.

Where is the error ? Thanks

If you want to get an object on a child by targeting the parent object you would want to call FindComponentInChildren

FIXED :smiley:

I’ve read that Unity 4 handles active totally different, so I fixed it using:

playerPrefab.GetComponentsInChildren<testvar>(true)[0].great;

Thanks :smiley: