Where do components get added in Unity?

by SanMalik   Last Updated December 06, 2018 11:13 AM

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?

Tags : unity c# scripting

Answers 1

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.

December 06, 2018 10:55 AM

Related Questions

Updated April 23, 2015 21:05 PM

Updated April 08, 2015 18:05 PM

Updated April 14, 2015 20:05 PM

Updated April 20, 2015 22:05 PM

Updated May 17, 2015 21:05 PM