I am very new to programming and struggling with a problem. I am trying to access the variable “score” from one script and use “score” to determine the height of a guitexture on another script. I have tried it a couple ways but i keep getting this error: Null Reference Exception: Object reference not set to an instance of an object.
This is how my script is written.
//this is my first script, which is named: stonyScript
var score : float = 0;
function OnCollisionEnter(col : Collision){
if(col.gameObject.name == ("asteroid")){
score += 10;
}
//here is the second script.
currentHeight = stonyScript.js.score;
function Update(){
guiTexture.pixelInset.width = 20;
guiTexture.pixelInset.height = 10 + currentHeight;
}
You have to tell it where to get stonyScript from.
stonyScript = GameObject.Find("Insert the name of the object that stonyScript is located").GetComponent(stonyScript);
currentHeight = stonyScript.score
I gave that a try but I am still getting the same error for some reason. Did i implement your method wrong?
stonyScript.js = GameObject.Find(“Stony”).GetComponent(stonyScript.js);
currentHeight = stonyScript.js.score
The easiest way I believe is to just make your variables public static if you want to easily access them.
for example:
Script #1 is called PlayerMoveScript and contains:
public static int playerHealth = 10;
You can access this variable from any other script by using:
PlayerMoveScript.playerHealth += 1;
or:
int newPlayerHealth;
newPlayerHealth = PlayerMoveScript.playerHealth;