Hi.
I have 2 Java script files where i need to pass a value from one file to another.
These are my extacted of my 2 files:
CharacterSelector.js
selected = 0
CharacterOne.js
if(selected = 0) {
fireMode = "enabled";
}
I need to pass "selected = 0" from characterSelector.js to CharacterOne.js, I have tried using:
selected = GetComponent(CharactorSelected).selected;
but it does not work.. Any kind soul be able to help?
First you need to find the object that contains the other script, you can use find for this. Then you can either use GetComponent or SendMessage like so:
other = GameObject.Find("CharacterOneGO");
other.SendMessage("SetFireMode","enabled"); // assuming you have a method called SetFireMode
or
other.GetComponent(CharacterOne).fireMode = "enabled";
or
other.GetComponent(CharacterOne).selected = 0;
P.S. it's better to use boolean values instead of strings if you simply need an on/off value
system
March 20, 2012, 8:18pm
3
I think it can be done in more easy way simply declaring Global variable.
CharacterSelector.js
static var selected: int=0;
CharacterOne.js
var mySelection;
function Update()
{
mySelection=CharacterSelector.selected;
if(selected = 0) {
fireMode = "enabled";
}
}
See details more about this in http://unity3d.com/support/documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html