If I have a GameObject that its invective by default. When I start the game/scene, Does the scripts get called? Does unity load it and then set it inactive?
3 Answers
3If it is set inactive in the editor the scripts attached to it will not run. It will use the Awake() and Start() function only the first time it is activated, regardless of when this happens. If it is set inactive by its own script, it will run only to that point.
I have Just tested this, and it seems that the scripts are not called on an inactive object.
Awake() will be called if the script is inactive, not the GameObject holding the script.
This is evident in this short example:
yes it does. the awake is called regardless the state. Start is called on first run, OnEnable any time you switch on or off Some of the callbacks are waiting on the events and will be called regardless the state. Drawing and setting off is no solution since drawing is performed after all updates and if set off it won’t draw.
Waiting time on start is no real issue so create them all at the beginning and put a loading screen to hide.
I'm trying to understand what is the advantage of setting it inactive in the editor. Let's say that I want to publish my scene with half of the 3D models active. Will it take the same time to load the scene if they all where active?
– Rotary-HeartLoad time is not as important as framerate. Performance (framerate) is hurt by what is active and unaffected by what's hidden. You keep asking about load time which is when objects are initialized and only happens once each scene. Open Unity, load in about a thousand objects, parent them to something, and test the load and run times with it hidden and viable. just testing it yourself is often the most accurate answere.
– NicRuleI know that it happens once, that's exactly why I'm asking this. Let me try to explain better, if I have a scene with a 100 heavy 3D models that are set to inactive in the editor. When I open/run my scene does Unity loads them even if they are inactive? Or Unity ignores them until I set them to active with a script?
– Rotary-HeartI just tested it with some massive meshes. With them visible it took 3.2 seconds to load. With them hidden 2 seconds. With them deleted it loaded in 1.1 seconds. That's the practical answer. I don't know what Unity does behind the scenes.
– NicRule
So, if I have like 100 GameObjects (its just an example) full of heavy 3D models but they all are inactive my scene should run/open fast because they are not active?
– Rotary-HeartI just tested it and it takes about the same time to load even if the objects are turned off. It will take slightly less time but it still has to process a lot of objects.
– NicRuleLike if it draws them and then hides them?
– Rotary-HeartDrawing then hiding is less efficient because it has to go through the process of hiding each object.
– NicRule