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
    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. – Philipp Dec 6 at 10:39
up vote 0 down vote accepted

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.

  • Was gameObject.renderer ever actually a storage location? I thought it was just a getter property that called GetComponent or somesuch internally. – DMGregory Dec 6 at 15:24
  • @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. – TomTsagk Dec 6 at 15:46
  • In either case, those fields have been deprecatedin favor of GetComponent. The only one that stays is transform – Draco18s Dec 6 at 20:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.