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 angelus_1984 · Mar 19, 2014 at 03:25 PM · gameobjectchildrenactivation

Why is the wrong child activated, although I have the right index?

Hello everybody,

I have Instantiated a list of Prefabs, with several Gameobjects as Children, in the Awake part and deactivated them. I want to randomly activate them during the game phase.

It actually works but the wrong GameObject is activated.

Part of my Code is this:

 for (int i=0; i<2; i++) {
                 randColor = Random.Range (0, numColors);
                 gameStats.correctColor.Add (randColor);
                 randShape=Random.Range(0,2);
                 randTexture=Random.Range(3,6);
                 if(i==0) {
 
                     indicator[i].transform.GetChild(randShape).gameObject.SetActive(true);
                     gameStats.correctTexture.Add (randShape);
                     indicator[i].transform.GetChild(randShape).renderer.material.color = gameStats.myColors [randColor];
                                 
                 }else{
 
                     indicator[i].transform.GetChild(randTexture).gameObject.SetActive(true);
                     gameStats.correctTexture.Add (randTexture);
                     indicator[i].transform.GetChild(randTexture).renderer.material.color = gameStats.myColors [randColor];
                             
                 }
             }

I use "randShape" and "randTexture" as the first should only get a random number of the 3 Child GameObjects and the rest a random number of the last 4.

However I have added some debug code and I get the following code in the console and the result in the manager:

alt text

As You can see for the first prefab index is 0 and randShape is 0 and the first Child Object in the Hierarchy is activated. So everything as it should be.

BUT for prefab index 1 and randShape = 4 the 2nd Child Object is activated instead the 5th one.

Anybody an idea why?

Thanks for the help?

-Gerd

unity screen.jpg (105.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ArkaneX · Mar 19, 2014 at 04:14 PM

You can't rely on an order of children in hierarchy window. They are sorted by name, and their order might be different than the order of internal objects collection.

To achieve the desired result, you either have to keep children info in some additional list, which is properly sorted, or you can retrieve all children, sort them by name and select correct one. I guess you will be calling this code from time to time only, so second solution should be ok.

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 Owen-Reynolds · Mar 19, 2014 at 04:47 PM 1
Share

Found this out by accident, the "official" child order, that the code uses, is the order they were added as children. So, to make "order you see" the same as "order computer sees," remove all children (I drag them into an empty, to keep them together) and re-add them in order.

avatar image ArkaneX · Mar 19, 2014 at 05:07 PM 0
Share

Agree, though one has to remember to repeat the operation after another child is added, which might break the order again.

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

22 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

Related Questions

Is it possible to get the width (in unity units) of a GameObject including all of its children? 1 Answer

Best Way - Animation - Multiple GameObjects. 0 Answers

Recursively get all children 1 Answer

use list of gameobjects transform in raycast 0 Answers

how to control spriterRenderer in childrens 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