Sign in to follow this  
Psilobe

Unity Unity c# scripting acsessing components and understanding the hierarchy

Recommended Posts

Psilobe    193

I'm using unity combined with c# but I'm having trouble with navigating the hierarchy from within my scripts to get access to specific components.

 

For example my hierarchy looks like this:

 

 

Ship

   Box01

   Box02

     Cylinder02

   Cylinder01

   Main Camera

 

 

Box02 got a component in which needs access to the camera component in the Main Camera. 

 

I've figured that what I need to do should be to somehow navigate to the Main Camera gameObject and then use the getComponent function but I'm not sure how to do it. I tried getting to the Ship gameObject by using transform.parent but that didn't seem to work.

 

I've been trying to find a good resource which explains this but I cant find one.

 

 

I finally found something that helped me.

 

http://answers.unity3d.com/questions/125847/trouble-accessing-child.html

 

The first answer here helped my immensely so if anyone else runs in to similar trouble then go here.

Edited by Psilobe

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this