user2966992
user2966992

Reputation: 13

Access a variable from another script using GetComponent

In unity (the game engine) I have two scripts

CharacterSelect.js
CarCameraScript.js

I want to access the variable

selectedPlayer

FROM characterSelect.js IN CarCameraScript.js

That variable currently looks like this:

var selectedPlayer : int = 0;

From what i hear is it has something to do with getcomponent. Any help would be appreciated

Upvotes: 2

Views: 5762

Answers (1)

Dasu
Dasu

Reputation: 433

you must create an instance of characterSelect.js in CarCameraScript.js

write in CarCameraScript.js

var characterSelectInstance : characterSelect;

characterSelectInstance = GameObject.Find("Name_Of_GameObjct_Where_you_attached_characterSelect.js").GetComponent(characterSelect);

so you can use it in CarCameraScript.js

var xyz : int ;
xyz = characterSelectInstance.selectedPlayer;

Upvotes: 2

Related Questions