How is Instantiate work with Start and Awake? ( I don’t think I found many topics related to this )
I have a script attached to a prefab, that I instantiate and destroy frequently. Does the Start/Awake function get called every time it’s cloned or just once? When you have many function calls to things like Find within those functions, I do not want them called every time.
I might have to rethink how equip items.
Basically, the way I have it set up is a small array of boolean values that represent if the player has an item or not. Each time the player changes weapons, it loads from a prefab the item, and destroys the old one. Each item is unique from the other, so I have appropriate script attached to each item prefab. I’m starting to wonder if I did this right. I don’t want to be running performance expensive calls each time I instantiate something.
Nope, I just got bit by this to the tune of several hours of frustration.
When you call Instantiate on a prefab, the Awake() function is run immediately, but NOT the Start() function. The Start function is run sometime later before the first call to Update().
So for instance, if in the Start() function of some other behavior you Instantiate a bunch of prefabs in a loop, each of the Awake() functions for each of the prefabs will get run as you go. Then after you finish from your Start() function, the Start() function of all the prefabs will get run.
Well that’s how it’s supposed to be and documented. That’s not the question as far as I can see though.
@Skeletim
Why are you actually destroying and instanciating things on every change? Why not just disable the relevant parts and use initializing methods? Note that destroying and instanciating will both assign and free memory once called, and the GC needs to do its work too. If you could just keep what’s there you’ll be left off with assigning once.
Heavy GC operations will be noticable by stuttering from time to time, just for example.