Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by donnysobonny · Oct 21, 2016 at 09:25 AM · componentgeneral programming

Is it okay to reference a child's parent?

Hey all,

So the question here is, if I have a MonoBehaviour (A) that references another MonoBehaviour (B), is it okay for B to store a reference to A?

I am aware that unity doesn't complain when and if the above happens, and it looks like unity's own MonoBehaviours implement this (try doing Debug.Log(this.gameObject.gameObject.gameObject.name) and you'll see what I mean). But I wonder if unity not complaining about this is just because it's a very common thing that developers might do. Does it in fact impact performance and as a result is something that we should avoid?

This question comes from a PHP background, where referencing a parent object from a child object can cause problems...

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by b1gry4n · Oct 21, 2016 at 09:32 AM

 Debug.Log(this.gameObject.gameObject.gameObject.name);

is no different than

  Debug.Log(this.gameObject.name);

your version is saying: this gameobject, this gameobject, this gameobject, name. its redundant. gameobject inherits from object and so does component. this is why youre able to do gameObject.gameObject.gameObject. Components are always attached to gameobjects.

https://docs.unity3d.com/ScriptReference/Component.html https://docs.unity3d.com/ScriptReference/GameObject.html https://docs.unity3d.com/ScriptReference/Object.html

Referencing a parent from a child doesnt cause problems. If you think of child gameobjects in more of a folder structure it might be easier to understand. You could have any number of child objects referencing the same parent and its not an issue

So to answer your question...it is ok to have child monobehaviours store parent monobehaviours. You see it everywhere, many people do it, I have never heard or had any issues regarding this.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image donnysobonny · Oct 21, 2016 at 09:41 AM 0
Share

Great, thanks for clearing that up :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

AddComponent changed after upgrading Unity 1 Answer

Make Script Component Do Things Even When Game Is Closed 2 Answers

Character Movement component 2 Answers

GameObject stutters when moving if refresh rate is not set to 60hz. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges