I am trying to understand an error I am getting with two scripts attached to the same game Object.
I have a main GameObject with mainscript attached.
I have also another script called script2 attached to the same object.
mainscript
Debug.Log("mainscript loading");
var myscript2:script2;
myscript2=gameObject.GetComponent("script2");
myscript2.run();
script2
Debug.Log("script2loading");
var myArray=new Array(0,1,2,3);
Debug.Log(myArray.length);
function run(){
Debug.Log("running");
Debug.Log(myArray.length);
}
The output in the Debug.Log is
*mainscript loading
running
0
"script2loading"
4
So mainscript is calling a function in script2 before myArray exists?
I don't get it.
Dan
Also this editor sucks. I hit italic and it takes out all my carriage returns.
Correct. No Start function is guaranteed to run before any other Start function. Same with Awake and Update and LateUpdate.
And since your code appears to not be in any function at all, I'm not exactly sure how Unity treats it (before Awake is my guess), but it's not guaranteed to be run in any particular order.
However, all Awakes are called before any Start is called, and all Starts are called before any Update is called, and all Updates are called before any LateUpdates.
So you could have Script2 initialize itself in Awake, and have MainScript muck about with it in MainScript's Start function, and you should get the functionality you're looking for.
I ended up adding an Init function to one of my classes, and the class throws errors if that isn't called before the class is used - that way I won't forget to initialize it and I won't accidentally use it uninitialized.