Detect if an object has been attached to the scene (C#)

Hello!

I’m new to Unity, C# and VR development, and I come from a web background.
I’m designing a World Space UI Canvas and working with VRTK to interact with the UI elements using ray casting. I simply need to be able to toggle the visibility of a Game Object based on "click"s on another Game Object (a UI Button).
Very often, I find myself checking to see if a Game Object has been attached to the scene before I query it and work with it. So my code is sprinkled with if (myGameObject != null) { /* do something */ }

Is this an anti-pattern? Is there any equivalent to the Web’s beloved onload, domContentLoaded or $(document).ready /* jQuery */ in Unity + C# land? I simply need to know deterministically if the Game Objects I need have been attached.

Thank you.

I think what you want is OnEnable() => “Sign me up”, “I exist”

Personally as a coder, I would make a Singleton class. Like Manager class, every object that need tracked will called OnEnable “I exist, manager add me to the list of important stuff”. Then you just have to call Manager.Instance.DoesMyStuffExistYet