Hide gameobject on scene startup

Hi,

quick question on deactivating gameobjects and execution order: I need to hide gameobjects that are placed and visible in editor, but should be initially hidden when scene comes up. So I set them to “inactive” in Awake(), but when the scene loads, those gameobjects are still shown for probably almost a (felt) second before the Awake() takes place and hides them by deactivatin. Is there any way to programmatically deactivate/hide a GO before it is shown at all?

Thanks, habitoti

Maybe i’m oversimplifying it, but why don’t you simply mark them as inactive in the inspector from the get-go. Obviously requires you to setup the references beforehand since you can’t ‘find’ inactive GOs, but it’s a way to do it.

there is another way, which is you move them to very far away position, like x=-1000,y=-1000,z=-1000 they are visible, but away from camera… so when you want them back, just adjust their position when you want them to appear again.

If that’s what it needs, then I’ll have to do it, but that’s not ideal in my case. For one, I’ll need those objects in editor all the time for alignments, rearrangements etc., and secondly – as you say – I can’t setup the references dynamically, which I wanted to avoid, so I wondered how it would be possible to execute code before the first items are put on screen…

Both Start() and Awake() are executed before the first frame is rendered, so you can do gameObject.SetActive(false) in either of those. Remember that the Game window is rendering before the game actually starts. I do the above fairly commonly and have never had an issue. Are you getting the behaviour you describe in the Editor or in a build?

Haven’t tried build yet. I ignored it for a while when testing the scene straight, because I thought it where just artifacts from starting it directly in the editor, but now I saw that it also happens when transitioning from another (menu) scene to the game scene.
Will try a device build to see if that makes a difference…

I was writing a line of code about this a few hours ago :slight_smile:
What I did was to disable Mesh Renderer of the object in the Inspector panel. With this way, I can hide the object from render engine at the beginning. If you want to reactivate it, you can use objectName.renderer.enabled = true;

//Show Object
hideShow= GameObject.Find("objectName");
hideShow.renderer.enabled = true;

//Hide Object
hideShow= GameObject.Find("objectName");
hideShow.renderer.enabled = false;

Hope it helps!

This sounds like a pretty slick idea, at least solving half of the issue :wink:
I am still missing the items in the editor then for arranging stuff (and not forgetting that they are there when placing other things).

I can confirm that it also happens on the device. So it does not seem to be true that Awake() of all GO scripts is called before the first frame of the scene is rendered…it is actually a painfully long time that passes before the GOs disappear after being set to inactive in Awake()…