Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
4
Question by Mr0w3m · Feb 03, 2016 at 06:10 PM · c#scripting problemgetcomponent

Accessing the sibling of an object

Hey guys, this may be entirely too simple of a question but for some reason everything I try isn't working. I'm shooting a raycast at an enemy who has a collider that is a child of the main object. So when I cast the ray, it returns the out hit and I can call GetComponent();

I need to get a script that isn't attached to the collider object, nor the parent, I need to access a sibling of the object I've hit. Is there any way to call GetComponent on a sibling? ![alt text Trying to access MouseLook from the Collider.

[1]: /storage/temp/63191-capture.jpg

capture.jpg (14.3 kB)
Comment
Add comment · Show 3
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 vintar · Feb 03, 2016 at 06:17 PM 1
Share

You could do a GetComponentInChildren on the parent.

avatar image Mr0w3m vintar · Feb 03, 2016 at 06:19 PM 0
Share

The hit goes to the Collider GameObject so I can't do it on the parent because It's not on the parent... Right?

Well maybe I could just do, temporaryHit.parent.GetComponentInChildren

avatar image vintar Mr0w3m · Feb 03, 2016 at 06:24 PM 1
Share

That's exactly what I meant

3 Replies

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

Answer by Mr0w3m · Feb 03, 2016 at 06:29 PM

That was it!
Script script = temporaryHit.parent.GetComponentInChildren
Thanks!

Comment
Add comment · Show 2 · 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 isteffy · Sep 27, 2016 at 07:59 AM 0
Share

Downvote. This does not access the child, only a component in the child

avatar image MMOERPG isteffy · Feb 04, 2017 at 04:08 AM 1
Share

Transform transform = temporaryHit.parent.FindChild("Child Name");

OR

Transform transform = temporaryHit.parent.GetChild(0);

GetChild(0) gets first child (1) gets second etc.

avatar image
3

Answer by unity_IAZAm8FPo3W9Uw · Aug 02, 2018 at 07:44 AM

You can get the next child of the same parent by using the GetSiblingIndex like this:

 SomeObject.transform.parent.GetChild(SomeObject.transform.GetSiblingIndex() + 1);
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
avatar image
2

Answer by Hoorza · Jan 26, 2019 at 02:18 PM

That is the easiest way for me:

 someObject = transform.parent.gameObject.transform.GetChild(0).gameObject;
 

The number in the brackets next to the GetChild() part is the index for the children of the parent object, so in this case, it will access sibling object that is the first child (index number 0), increase index number to access next child in the hierarchy. Hope it helps someone as it took me a while to get it right. Cheerio!

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 Berlm · Jul 18, 2020 at 06:33 PM 0
Share

Hey I dont know why I am doing this because I am very late, but instead of doing

 someObject = transform.parent.gameObject.transform.GetChild(0).gameObject;

You can do:

 someObject = transform.parent.GetChild(0).gameObject;

The reason is that transform.parent or transform.GetChild (all types of these methods) return a transform. In your code, you are getting the transform's parent (which will give the transform), then getting the gameObject of the parent and then getting the transform from the gameObject.

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

82 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 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

Randomized array result from other scipt 2 Answers

Changing script variable from another script doesn't change it in the original script? 0 Answers

NullReferenceException on getComponent 1 Answer

C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer

C# > Camera tagged MainCamera > GetComponent<[ScriptClassHere]>() returns null ? 0 Answers


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