Hi, I have 2 Scripts globalproperty.js and display.js. I am accessing the value of variable of globalproperty.js in display.js but its failing, display.js script is executing first. How can i make globalproperty.js script to execute first.
//globalproperty.js//
var number: int;
function Start()
{
number = 4;
}
//display.js//
var global : globalProperty;// instance of globalproperty script
function Start()
{
global = GameObject.Find(“Cube”).GetComponent(globalproperty);
Debug.Log(global.number);// Here its printing 0 instead of 4. It means this script is executing first
}
I want to execute globalproperty.js first, what should i do now?
One way would be to initialize during the declaration:
var number:int = 4;
Arranging the start up order would be a pain in the neck, but there is probably some way of doing it. For instance, starting the scripts not activated and then activating them in the order you choose. Since it’s a start script, and you know the init, it doesn’t make a lot of sense to me. Why not just init the other script manually, unless one object is going to be destroyed and the other not.
i tried initializing the variable in the declaration but also i got same result…
I wouldn’t bother. You can never guarantee which script will be executed first.
There’s two ways I usually solve this problem. The first is to put a WaitForSeconds in the display.js Start function. Usually waiting for 0.1 seconds is enough.
The other solution would be to have the globalproperty.js script call the display.js script’s Start function directly once the variable has been defined (although I would change the function name from Start to something else.
A third option would be to make the number variable a static var instead, and then define it in display.js by using:
display.js
void Start(){
globalproperty.number = 4;
}