Possibilties to acces var`s in other gameObject`s scripts

Hi :slight_smile:
I searched the web after such possibilities but i didn`t find anything.
How do you change or reed a vareable in a Javascript of another gameObject?

I mean… there must be a way for gameObject`s to kommunicate, or not?

of course you could slove it like this:

Javascript on an enemy-object:

var score:Transform;

function OnCollisionEnter (collision:Collision) {
//if hit by a projectile
if(collision.gameObject.name==“projectile”) {
//add 5 to score
score.transform.position.y+=5; //so the score-vareable is the y-possition of the gameObject named “score”
//delete self
transform.active==false;
}
}

But i think there must be a smarter way!

Thanks for your attention :wink:

I find it hard to believe you wouldn’t find anything with a search, since this is easily the most-asked question and has hundreds of answers. See the docs, specifically section 3. Also, http://forum.unity3d.com/threads/143875-Using-code-tags-properly

–Eric

The scripts communicate (not the game objects).

So one script on one game object would send information to another.

You can get the script attached to a game object using GetComponent (look it up in the documentation).

It’s not a good idea to use position or something to store variables. Here is a suggestion.

(this is a quick way of doing this… there are other better ways… btu this

  • Make an Empty Game Object… put it at 0,0,0. Name it something such as “Global”
  • Make a script name it something like “scorekeeper” attach it to the Global game object. Make a variable in there called “score”

When you want to add a score you can do something like this

GameObject.Find(“Global”).GetComponent(scorekeeper).score += 5;

Thanks for your answer Eric.

I think i can continue scripting now :slight_smile: