Unity 3, Singleton access problem appeared

Ok I’ll post it here for more visibility.

I got some problem with access to static typed objects in U3, while I hadn’t any with UnityiPhone 1.7 :

Let’s admit this sample :

_component1 will be output as “null”.
This is not logical as Awake() is called at script instanciation, so component1 should be available inside myClass from any script as soon as it is added ? It’s like the Awake() call momentum was changed with U3… weird.

This is really problematic, as it prevents from direct accesses.
Plus I had whole my framework revolved around this direct access (as said, it didn’t make any problem with iPhone1.7), because Singleton had in some cases better performance than class instancing.

Anyone had this problem ?

I’ll file a bug report if necessary.

this is very well logically explainable because within awake you should never expect the rest of the world to be fully there. try it within start and the gameobject find should work preventing you from failing.

that or store a reference to the object you want to use

Ok thanks Dreamora, I’ll change that.
But how did this work in previous Unity versions though ?

By pure luck ;-). It is written somewhere in docs that order in which Awake/Start is called for objects is undefined - the only thing you can be sure of - when you are in Start method, Awake was called for everything else in the world.

Ha, thanks to both of you then,

I can deduce that I’m a very, very lucky man to have a whole 10000 lines framework working by chance :stuck_out_tongue:

it worked before because the object init order potentially was different. Generally its not defined in which order the awake calls on the objects are done its only defined that all objects in the scene will do awake before the first will do start and all will do start before the first will get update called.

I normally split the code like
Awake: Local init on me and potentially same game object
Start: relation ship init with other components, especially on other GOs

Yeah, seems like order changed. Thanks for the tips, though, but I won’t use any Awake/Start anywhere than my Superclass if it’s this reliant :expressionless: