accesing other script with GetComponent

Ik have a script attached to the main camera with a “controllerscript” attached an inside there a public variable score

No in another script i want to acces that variables:

 GameObject thePlayer = GameObject.Find("ThePlayer");
           controllerscript playerScript = thePlayer.GetComponent<controllerscript>();
        playerScript.score += 1;

Now it gives me the error: NullReferenceException: Object reference not set to an instance of an object

Where are these lines of code? Within Start, Update, some other function? Or are they just not in one where variables get declared?

Take a moment to look at what the error means then it’ll be easy to figure out. In your case it means you didn’t get the object you were asking for and assuming you did because you just call stuff on a potentially NULL reference.

If there’s no a “ThePlayer” object that’ll return NULL so you then cannot access the “thePlayer” variable.

If there’s no “controllerscript” component on “thePlayer” or that you’re not usnig the correct case because C# is case-sensitive.

Please note for the future that this forum is the 2D forum for 2D features and not for any error you have while trying to make a 2D game unless it relates to a 2D feature. The General Scripting forum is here.