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
1
Question by Tasarran · Jun 03, 2011 at 07:26 PM · parentchildaccess

Trouble accessing child

I have this script on an object that is the parent of several objects that I want to access.

 Hex = gameObject.transform.parent.gameObject;
 startMarker = Hex.Find("Ball-Start/Cube");

This code only finds the first instance of Ball-Start/Cube in the scene, so multiple instances of the script end up controlling the same object.

How can I force this script to find only the Ball-Start/Cube that is attached to the group that it is a member of?

Comment
Add comment · Show 2
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 Tasarran · Jun 03, 2011 at 08:28 PM 0
Share

Wow, I really find it hard to believe that this is so difficult...

I can't simply navigate my hierarchy parent GameObject to child GameObject through some method?

avatar image Tasarran · Jun 04, 2011 at 02:40 AM 0
Share

I think I'm just going to try to re-structure the hierarchy so that all of these components are stored as components of the same object that has the controlling script.

3 Replies

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

Answer by Wolfram · Jun 03, 2011 at 10:19 PM

First of all you need to be aware that

  • anything you set to active==false can no longer be found via Find(), FindObjectsOfType(), or GetComponent...() (although the latter has an optional parameter that allows searching inactive components)

  • Find() in general is very expensive. So you should search your objects in Start() or Awake(), and store them in a variable. At the same time, this helps you to avoid the first problem, since once you have your target objects accessible via a variable, you can always access them, even when active==false.

Now, there are several methods to approach your problem. All of them start by storing the parent object containing the cubes in a variable (I don't speak UnityScript, so this is all C#, which is a better choice (=more powerful from a programmer's point of view) anyway, but all this is possible similarly in UnityScript):

 public GameObject parentObject;
 void Start(){
     parentObject=GameObject.Find("Ball-Path");
 }

(Instead of finding the object in Start(), you could also assign it in the Inspector manually.)

If the object named "Ball-Path" contains only cube objects you want to disable, your options are:

  • disable everything recursively starting from the parent: parentObject.SetActiveRecursively(false);

  • only switch off the renderers, so that the objects are no longer rendered, but remain active (note, here, too, you should instead store the objects in a Renderer[] array in Start, instead of calling the GetComponents... method every time):

       foreach(Renderer r in parentObject.GetComponentsInChildren<Renderer>())
             r.enabled=false;
    
    
  • If you only want to disable particular objects (e.g., named "Cube"), you can walk through the hierarchy with (note this loop will only find direct children of parentObject, you need to recurse yourself if you want to access the whole hierarchy this way):

       foreach(Transform t in parentObject.transform)
             if(t.name=="Cube")
                 t.gameObject.active=false;
    
    

EDIT: one more alternative:

  • In the worst case, if your objects can be anywhere in the scene hierarchy, but they are all called "Cube" (and no other obejcts are called "Cube"), you can still do this (however, very expensive in complex scenes):

       private Renderer[] allRenderers;
         void Start(){
             allRenderers=Object.FindObjectsOfType(typeof(Renderer)) as Renderer[];
         }
    
         void DisableCubes(){
             foreach(Renderer r in allRenderers)
                 if(r.gameObject.name=="Cube"){
                     // found one! set object to inactive, or disable the renderer, as described above
                 }
         }   
    
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 Wolfram · Jun 03, 2011 at 10:28 PM 0
Share

Also, see here: http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html

avatar image Tasarran · Jun 04, 2011 at 06:12 AM 0
Share

I used a combination of looping through the child transforms in Start and setting vars and using SetActiveRecursively on those vars.

avatar image
0

Answer by almo · Jun 03, 2011 at 07:31 PM

If it's a component, and there is only one in the group, you can use:

http://unity3d.com/support/documentation/ScriptReference/Component.GetComponentInChildren.html

If there might be many in the group,

http://unity3d.com/support/documentation/ScriptReference/Component.GetComponentsInChildren.html

will return a list of them.

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 Tasarran · Jun 03, 2011 at 07:41 PM 0
Share

No, it's not a Component, Ball-Path is an empty GameObject, with the GameObject Cube grouped under it.

Cube has a mesh and a light Component. I want to toggle active on the Cube GameObject.

avatar image
0

Answer by DaveA · Jun 03, 2011 at 09:03 PM

Some ideas:

GameObject.Find(transform.name+"/Ball-Start/Cube");

Or use Tags

Or use specific names for the Cube

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 Tasarran · Jun 03, 2011 at 09:25 PM 0
Share

That won't work, since the parents are instantiated multiple times. Even referring to the whole path by name all the way up to the top might not be unique.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Access a child from the parent or other gameObject. 2 Answers

Accessing a variable from a child object and pass it to the parent 2 Answers

Best way to Reference Child from Parent's script? 3 Answers

Access parents other child from other other child 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