What is the scene really and what does correspond to? Under the hood, does the scene in the editor get converted to some object declaration code, with objects in the hierarchy corresponding to the variables in the declaration? Then when the game runs, the main game loop could first instantiate the scene and its objects (with proper initialization).
In more detail - I can imagine that the scene we’re in contains objects with specific names (those that we can see in the scene hierarchy). I would assume that whenever an object is created in the hierarchy, its given some sort of unique code (not necessarily corresponding to its name), and this object is declared in the scene code. Is that right? Is it an object, and are the objects in the hierarchy its fields?
It could be less intuitive - I can also imagine that the scene is some type of a method, and objects in the scene are its local variables.
Each Monobehavior is an object that goes through this life cycle:
Unity itself is the backend handling all of that (and more). For that, yes there is somewhere a class to keep track of all the references. I’m not too sure how exactly it is implemented, but is that really important? If you know how object oriented programming works outside of Unity, then you should have a general idea for how it works in Unity. Monobehaviors are just some subset of objects afterall. There is heavy caching and other optimizations going on to improve the performance, but generally speaking, you can think about all of that as a black box.
In case that’s your question:
A GameObject is likely also just some object that has a list of components, for example of type Monobehavior.
I could probably give a more detailed answer if you are looking to understand a specific topic, but if this is just a broad curiosity question, there is too much going on to explain it in one post (and too much i’m not comfortable explaining).
If this is not purely for curiosity reasons tho, then you can really think of it as a black box.
If this is about performance, there are a couple of ressources provided by Unity and externally on that topic.
Hope this helps a bit, not sure if i really answered your main question tho, so sorry if this is not what you are looking for.
Hi, thanks. Yes I have a specific topic in mind - I just made this question first, because the more specific topic is directly based on “how game objects are kept track of” (the topic of this thread). I didn’t want to mention that question in this thread too, because it would make the post too long and messy, so I decided I would ask two separate questions.
I would appreciate your feedback in my new question:
Scene are specific container, just like prefab, they hold scene specific data and likely a list of game object.
In fact I tend to bypass scene (outside of the first one) to create a gameobject that is a list of list of gameobject … don’t do that with unity if you are sane.