Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
3
Question by moghes · Mar 14, 2013 at 12:34 PM · gameobjectgetcomponent

Get the child gameObject of a parent and not the Transform

Hello everyone! I am stuck in a simple problem.. I have an empty-gameObject parent to a gameObject.

By script I would like to set different materials, I

 public var GOContainer:GameObject; // dragged the empty-gameObject (parent)
 
 function CreateShperes()
 {
     var i : int = 0;
     var objNumber:Number;    
     var obj:GameObject;
     
     while(i < objNumber)
     {
         var currentObjContainer:GameObject;    
         currentObjContainer = GOContainer;
         objNumber = Mathf.Round(Random.Range(1,4));        
         
         Instantiate(currentObjContainer, transform.position , Quaternion.identity);

             // obj = currentObjContainer.GetChild();
     // obj is the child (only child) of currentObjContainer

         switch(objNumber)
         {
             case 1:
             obj.renderer.material = redMaterial; break ;
             case 2:
             obj.renderer.material = yellowMaterial; break ;
             case 3: 
             obj.renderer.material = greenMaterial; break ;
             case 4:
             obj.renderer.material = blueMaterial; break ;            
         }
                     
         yield WaitForSeconds(0.3);
         
         i++;
     }
 }


Well I want the child gameObject and not the transform.. and the console tells me that GameObject is not IEnumerable ... please some hints.. Thanks

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 cregox · Nov 05, 2013 at 04:14 PM 1
Share

somewhat related: http://answers.unity3d.com/questions/205391/how-to-get-list-of-child-game-objects.html

4 Replies

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

Answer by moghes · Mar 14, 2013 at 01:41 PM

I solved this way, it might not the be optimal solution, but it works like a charm:)

obj = currentObjContainer.transform.Find("obj_Prefab").gameObject;

Comment
Add comment · Show 6 · 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 superme2012 · Dec 22, 2013 at 12:16 PM 0
Share

sweet, things like this make me happy ;)

avatar image Flailer · Apr 11, 2014 at 09:11 AM 2
Share

I know its been ages, but wouldn't it be better to go gameobject.transform.getChild(0).gameObject; the 0 here denotes the first child game object, this can be set however you please. But makes the Child and the object name independent. Just based off which object it is attached to.

avatar image cregox · Apr 12, 2014 at 04:27 PM 1
Share

@Flailer nope. It's also a bad idea, although a bit better than the idea in this answer. Both are a kind of hard coding where it isn't needed. But, please, do read my answer for more info! ;-)

avatar image Flailer · Apr 15, 2014 at 08:55 AM 0
Share

@Cawas totally didn't see your post. Good answer and makes sense.

avatar image Kerihobo · Dec 26, 2014 at 12:42 PM 0
Share

I made great use of your hard-coded method Flailer, thanks! Oddly enough it was exactly what I was looking for.

Show more comments
avatar image
3

Answer by cregox · Nov 05, 2013 at 04:28 PM

The game object hierarchy is always inherent to Transform, and never to the GameObject.

From your confused question and current answer, I'm guessing you have some Transform hierarchy which looks almost exactly like this, with just 1 child:

 obj_SphereContainer (parent)
    - obj_Prefab (child)

Then, you could have done simply this:

 obj = currentObjContainer.transform.GetChild(0); // gets the first child

But neither this or your current solution are actually good. You're locking up the game object hierarchy through script and that can bring many head aches if you decide to change the hierarchy - specially if you don't remember it was locked in this specific script.

You are looking for renderers inside the instantiated object. So, this is much better:

 for (var rend : Renderer in currentObjContainer.GetComponentsInChildren(Renderer)) {
    DoSwitch(rend);
 }

Furthermore, I'd clean up your script into this:

 public var prefabContainer:GameObject; // dragged the empty-gameObject (parent)
  
 function CreateShperes()
 {
     var objNumber:Number;  
     
     for (var i = 0; i < objNumber; i++)
     {
         objNumber = Mathf.Round(Random.Range(1,4));     
         
         Instantiate(prefabContainer, transform.position, Quaternion.identity);
         
         for (var rend : Renderer in prefabContainer.GetComponentsInChildren(Renderer))
         {
             switch(objNumber)
             {
             case 1:
                 rend.material = redMaterial;
                 break;
             case 2:
                 rend.material = yellowMaterial;
                 break;
             }
         }
         
         yield WaitForSeconds(0.3);
     }
 }
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
1

Answer by sradforth · Mar 14, 2013 at 12:42 PM

Have you tried just getting the child transform and from the transform using the .gameObject parameter of the transform?

See what I think is a very similar question here... Transform to GameObject

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
0

Answer by alok.kr.029.hotmail · Jun 11, 2014 at 06:56 AM

you need to modify this a little in case of TextMesh to gameobject this is best and work perfect for me

public TextMesh getChildGameObject(GameObject fromGameObject, string withName) {

     Transform[] ts = fromGameObject.GetComponentsInChildren<Transform>();
     foreach (Transform t in ts) 
         if (t.gameObject.name == withName) 
     {
         TextMesh to1= t.GetComponent<TextMesh>();
         return to1;
     }
     return null;
 }
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

16 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

Related Questions

enable and disable boxcollider (whats wrong with my script?) 2 Answers

Obtaining an array of positions from an array of gameobjects 2 Answers

A generic "Find GameObject From Path" method. 2 Answers

using a custom rotation in "Instantiate" 4 Answers

Only change a variable on the instaniated object not the prefab. 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