Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 $$anonymous$$ · Sep 16, 2014 at 11:39 PM · gameobjectcomponentreferencingattachment

Get Component that a GameObject is Attached To

I have GameObject A.

GameObject A has Component B attached to it. Component B is just a Script. Component B has GameObject C as a public variable.

GameObject C is just an empty GameObject with a Script attached to it.

How do I reference Component B from GameObject C? How do I reference GameObject A from GameObject C?

(I know this is a little confusing, and I apologize. I tried to simplify it as much as possible.)

Comment
Add comment · Show 1
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 Kiwasi · Sep 16, 2014 at 11:45 PM 0
Share

If I get this right you are asking to climb backwards up the references? This can't be done through normal program$$anonymous$$g methods. There may be some bizarre reflection techniques that allow this.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by rutter · Sep 16, 2014 at 11:48 PM

The structure you've described has available references like so:

 GameObject A
     ^
     |
     v
 Script B  ------->  GameObject C
                         ^
                         |
                         v
                     Script D

From A, you can find B.

From B, you can find A or C.

From C, you can find D.

Unfortunately, there is no reference from C back to B, which makes the question as stated difficult or impossible.

You may be able to find another solution:

  • Are A or B unique within your scene? If so, you may be able to look them up by tag, name, or singleton instance.

  • Can you add a reference from D back to B? You could set this reference in the inspector, or from a script.

  • Can B notify D that it exists? This would allow you to cache a reference.

Since you haven't described the scenario in much detail, beyond the available references, vague suggestions are about the best I can give.

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 $$anonymous$$ · Sep 17, 2014 at 12:00 AM 0
Share

That general answer is exactly what I needed to learn. Thank you very much! Very informative and helpful!!

avatar image
1

Answer by orb · Sep 16, 2014 at 11:49 PM

If A has a unique name (if it doesn't, how would it even be possible to get it?), get it like this in object C's script:

 GameObject aye = GameObject.Find("Object A"); // Replace with the name of your object, of course :)

Then get the component:

 ComponentB bee = aye.GetComponent<ComponentB>();

You should check that aye is actually not null before getting the component.

Comment
Add comment · 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

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

25 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

Related Questions

Can I move a component from one GameObject to another in script? 1 Answer

How to mimic Unity's Components based System 1 Answer

How to Check if a component Exists on a Gameobject. 3 Answers

Storing component of instance in an array 2 Answers

How to get a List of references of instances in a static variable 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