I have just noticed a habit I have, which is to, in general, ask for any GetComponents at Start(), not Awake(). I understand the difference between Start and Awake, but I am wondering if not using Awake is a steeper performance hit. I am still asking the same information, and amount, but just in a different frame.
Any difference?
How many thousand times a second are you calling Start?
I am not sure what you mean. It is called once, per script. So is awake tho. I am just wondering if placing all my get components and stuff, this var equals this etc would be better for the engine, in Awake. I am thinking it would be but am unsure.
The only difference is when those calls are made, otherwise no performance difference
Ok,
The only reason I mention is that my levels seem to take too long to load. I was wondering if by placing these in awake it would A) speed up the overall load time and B) ease up the sluggishness I experience moments after start. It usually takes a moment to begin to run at good speed.
The only performance difference I can think of is when
- you have many of objects,
- most of them are initially disabled,
- you enable them one by one as needed.
If you use Awake, all the objects get initialized at once at the scene startup. In case of Start, the objects get initialized only when you need/enable them.
So you recommend what and why?
Imagine, it takes 1 millisecond to initialize an object and you have 1000 objects in your scene and only 100 of them are initially enabled. If you initialize the objects in Start, you’ll get 100 ms delay at the scene startup. If you do it in Awake, you’ll get 1 second delay.
It’s up to you what method to use. You can also consider using OnEnable, it helps objects to survive script recompilation during runtime.
PS
There’s a good video about object initialization: http://video.unity3d.com/video/4929984/0/luug-11-pt-2-of-4
2:09
Ah, I see. So do it strategically in terms of objects that are enabled and not.
Is that your only concern in terms of Awake and Start? An objects activation? I am wondering about other things like getComponent, positioning, enabling certain features…
Richard Fine describes on that video pretty much everything I know about Awake, Start and OnEnable. So, if you watch it and still see no difference which method to use in your case, the most likely there is really no difference.
Another thing to consider is having functions that cache the information. Instead of cacheing it in Start or Awake, the function checks to see if it’s got a value. If no value, it finds the object/script/whatever and caches it. Then the function continues on with the cached value.
The benefit to this is that there isn’t a huge startup time, and the penalty for acquiring the data is spread out as its used.
The problem is that if you have everything pulled at the same time, your game will stutter when it happens.
In general, I say if it’s going to stutter because of it (lots of things at once), put it in Start or Awake. If it’s not, spread it out and don’t make the player wait up-front.
This is heavy optimization, though, so don’t do it blindly. Get things working properly and then worry about performance.