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 d112570 · Oct 01, 2013 at 11:12 AM · parentchildhierarchycountfind-gameobject

Count how many parents in Hierarchy?

Instead of using the inspector to add how many parents and children I have I would like to use the count method.

  1. Count how many Parents the Hierachy has, omitting Camera.

  2. How many Children does Parent "0" have. (Parent 0 would be 0-Star System)

  3. How many Children does child 7 have in folder called "Satellites". (Would be the Satellites of Saturn for example)

I would like to use this;

 parentScriptName = Hierachy.Split( '-' )[ 0 ]  // This would be the name for the script, like "0"
 parentUserName   = Hierachy.Split( '-' )[ 1 ]  // This would be the name for the GUI-User Info, like "Sun"

So I only get the number as name, anything after that is only for user info.

So when everything is set then I can start find objects like for example;

 GameObject.Find(parent0/child7); // Found the planet named Saturn

or

 GameObject.Find(parent0/child7/Satellites/child3); // Found the Satellite named "Thethys

Count is important, If I know there are Star Systems number 0,1,2,3 and Star Systems 3 has 6 planets then I can find object 3 + 6 so I can set the transform etc.

alt text

hierachy.png (17.3 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

2 Replies

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

Answer by Jamora · Oct 01, 2013 at 05:05 PM

If you want to traverse the Transform-hierarchy, then you need to use Transform.Find. You cannot search by partial names, so you will either need to rename your GameObjects to only numbers or iterate through all children using Transform.GetChild. Transform also implements IEnumerable, so a foreach loop will work as well.

So, you remove all names and only keep the numbers. When all is done, you could use

 GameObject.Find("0-Star System").transform.Find("3/6").gameObject;

Though be wary of GameObject.Find, it's not the most performant function available...

The easiest way I can think of to count how many star systems you have, is to have an empty GameObject at (0,0,0), call it "The Center of the Universe" maybe. Then have all the Star Systems as children of that. Finding the count then is as simple as

 GameObject.Find("The Center of the Universe").transform.childCount

Especially after having read your previous question, my gut feeling tells me there's a simpler way to model the universe.

Comment
Add comment · Show 4 · 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 d112570 · Oct 01, 2013 at 06:48 PM 0
Share

Yes, this will do, I will add a script to the numbers for the name, class, type etc, and then prefab it, afterwards you only drag and drop into a star system and have to do nothing else except tell the setup script how many planets or satellites it has. This sure helps a lot, thx again. Now I can go back to work, to make it user simple.

Only one thing bothering me now is, If I select what category object is like Star, I would then like inspector to show the star settings, and if I choose Planet, then planet settings. I tried this, but didn't work. Is something like this possible?

 public Category category;
 if (category == "Planet") public pType planetType;
 if (category == "Star")    public sType starType;
avatar image Jamora · Oct 01, 2013 at 07:05 PM 0
Share

It's possible, but you will have to create a custom inspector for that.

avatar image d112570 · Oct 02, 2013 at 12:37 PM 0
Share

Are there any differences from these 2 codes that do the same thing?

 var pSphere   = GameObject.Find("Galaxy" + cSystem).transform.Find("" + i + "/Sphere").gameObject;
 
 var pSphere   = GameObject.Find("Galaxy" + cSystem + "/" + i + "/Sphere");

I'd prefer the shorter form. I'm used to it from my previous scripts.

avatar image Jamora · Oct 02, 2013 at 12:39 PM 0
Share

If you know the full name of the gameobject, then the 2nd choice is probably faster. GameObject.Find is still quite a slow method...

avatar image
0

Answer by d112570 · Oct 02, 2013 at 04:39 PM

Hey thx, I was trying out the transform.count seems to work. I tried to implement the code got an error. There is a way to fix it but not sure how. Not all planets have Satellite folder so the count result would be null, and this null spits out an error. Is there any way to turn the null into a integer 0? It is the red null refernce exception error. Another question is, is the count already an integer? Thx David.

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

15 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

Related Questions

How do I find a child object in a hierarchy of children? 1 Answer

Make a simple tree 1 Answer

Change an object's grandparent 2 Answers

How can I access a Collider's GameObject in script? 1 Answer

what happens when Unit in Hierarchy is destroyed? 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