changing variable in other script

it is probably stupid question, but why this works:

function OnMouseDown () { GameObject.Find("MainScript").GetComponent(Script1).score = GameObject.Find("MainScript").GetComponent(Script1).score+1; }

and this don't:

function OnMouseDown () { var aaa = GameObject.Find("MainScript").GetComponent(Script1).score; aaa = aaa+1; }

Because aaa is a value type and you're adding +1 to aaa, not to the score-field you want.

Besides that, you really shouldn't use GameObject.Find the way you do (it's slow!).

Try this instead:

var myScoreScript;

function Start()

function OnMouseDown()
myScoreScript+=1;   // or "myScoreScript.score=myScoreScript.score+1;"

I don't code in javascript, so I don't know if += works, but the long version does in any case.

Note: It's always good style to setup and store references like "myScorescript" in Awake() or Start() and use the stored references during the game.


look at the example below:

In this case the script name is HealthControl.js and inside is a variable called LIVES.

HealthControl.LIVES = 3;

Simply use the script name followed by a dot and the name of the variable you want to change the value.

Hope this helps...

Please vote for me if you find a good answer