Not sure why I’m having such a hard time finding a simple answer for this, how do you find an inactive game object? Trying to make a pause menu but I can’t get the UI elements to work correctly since I can’t find it when it’s inactive. I’d rather assign the reference via script than the inspector since it’s less likely to get broken this way… thanks
Other way round. Once you serialize the reference in Unity via the inspector, the only way it’ll get lost is if you delete the object in question. It’s always the safer bet.
As for finding it via script, FindObjectOfType has an optional “include inactive” boolean parameter: https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
Ahh, ok. Ive just had the inspector do weird stuff so prefer to do it via script to avoid that. And I wasn’t sure about using FindObjectOfType because I thought i read somewhere it was a little more costly (unless I’m mistaken).
That being said, I did get it working just by assigning a reference via script, I just had to set it to false in my start, that way I could just set it as active in the inspector but it would set itself to false immediately after instantiation. Thanks!
It’s my understanding that FindObjectOfType is the most performant way to look through the entire scene because it leverages the internal lookup table of components that Unity has. If you don’t need to look through the whole scene, GetComponent(InChildren/InParent) would be faster, but you’d need to use the GetComponents versions to find inactive ones, which comes with some allocation overhead as well as finding all the components when you only want one. GameObject.Find is abysmally slow because it has to evaluate every single GO in the scene one by one (with a string comparison no less) until it finds a hit. Avoid that one as much as you can.
I’d personally try to serialize all references in the editor, as they’re free from a performance perspective, and use just four bytes of memory.
Also works: you can leave the game objects active when build, initialize in Awake, store the reference in code and make the game object inactive in awake.
I often just have another object which holds the reference to the inactive object. Whenever a script needs to find the inactive object, it gets it through the active object with the reference. This reference to the inactive object could be established via the inspector, or if it is an instantiated prefab it will typically be the script which instantiated it.
Yeah, thats what I ended up doing. Thanks!
I specifically asked how to assign the reference only via script (not through the inspector) just because changing things around i don’t want to mess myself up, would rather just have it script controlled so I know for sure. But I’ll keep that in mind about prefabs as well, thanks!
I’m still learning about serialized fields and how to properly use them (actually after I get my pause working the next thing I need to do is implement save data) but will definitely keep all that in mind as well, thanks!