In unity's GameObject class, there are a few default data members. When we add a component to the game object, light for example, where exactly does it get added? Does it become a part of the GameObject class?
-
1\$\begingroup\$ It is not really clear to me what you are asking here. I am not sure what kind of "default data members" you are talking about. It might help if you try to tell us more about the problem you hope to solve with this question. \$\endgroup\$ – Philipp Dec 6 '18 at 10:39
In the past, components used to be added to the GameObject itself, in such a way, that you could call myGameObject.renderer
to get an object's Renderer
.
This however changed to the call of myGameObject.GetComponent<Renderer>()
, which hides the details of where the components are saved, which shouldn't be relevant anyway. Since for most (if not all) cases it doesn't matter for the user where components are, but that they belong to an object.
As @DMGregory noted, when you (used to) refer to a component with the first way (myGameObject.renderer
) it acts like a getter behind the scenes, so the GetComponent
function just replaced that, it still seems to work the same way, as in, you can get a component from a GameObject
but the implementation details are unclear and not needed.
-
\$\begingroup\$ Was gameObject.renderer ever actually a storage location? I thought it was just a getter property that called GetComponent or somesuch internally. \$\endgroup\$ – DMGregory♦ Dec 6 '18 at 15:24
-
\$\begingroup\$ @DMGregory I think you are right it's not a literal storage location (so you can't do
myGameObject.renderer.variable = 5
for example), Unity does a lot of things behind the scenes, especially with readonly variables, that it's hard to tell what counts as a storage location. \$\endgroup\$ – TomTsagk Dec 6 '18 at 15:46 -
\$\begingroup\$ In either case, those fields have been deprecatedin favor of
GetComponent
. The only one that stays istransform
\$\endgroup\$ – Draco18s Dec 6 '18 at 20:31