- Home /
Global parameter, Count int. how do i do that ?
I guess for some of you a pretty basic thing, but i don't know how to do it.
i have Javascript A, and Javascript B.
within A i have a simple int parameter called countX this count registers a event, and counts ++ for each time the event happens.
in script B, i want to be able to "read" the current value in A. And for example, be able to make it -- (so it is also -- in A)
can anyone help me out ? thanx
Answer by jaised · Feb 25, 2012 at 06:21 AM
In your B script put the following:
var aScript : AScript;
function Start() {
aScript = gameObject.GetComponent.<AScript>(); // assuming both scripts are attached to the same GameObject
if(!aScript)
Debug.Log("Couldn't find ScriptA.");
}
And then wherever you want to reference countX, just put: aScript.countX
. Hopefully this helps.
Adding on from what @jaised said, if your scripts are attached to different objects, add
var objWithScriptA : GameObject;
to start of script. Add gameobject with script A attached in inspector, and then ins$$anonymous$$d of aScript = gameObject.GetComponent.<AScript>();
make it
aScript = objWithScriptA.GetComponent.<AScript>();
There you go. You're all set!
It's a bit faster if you do GetComponent(AScript)
rather than GetComponent.<AScript>()
, plus it's easier to type.
And even faster and easier is to drag&drop the script itself ;)
Ins$$anonymous$$d of
var objWithScriptA : GameObject;
just use
var aScript : AScript;
and drag the other gameobject onto that variable. Unity will assign the component reference of the right type if there is one. There's no need for GetComponent then.
Your answer
Follow this Question
Related Questions
Set script global variable value priority over editor values 1 Answer
gameobjects don't add to count when destroyed 2 Answers
Global update function for network 1 Answer
Touch point to world coordinate and local coordinate 1 Answer
How to rotate an object around the world axis but not on the local axis? 2 Answers