• 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 UniqueMAX · May 15, 2015 at 05:11 AM · parentchild

Parent error

Hello, I have a problem with defining parents in Unity.

I have an empty gameobject Town with 6 Building objects inside. During the game player places 10 Citizen objects inside these buildings and they become children each of their own building. During the game a random Citizen is picked and variable Location is defined as the Building where the picked Citizen is at the moment.

GameObject Location=Citizen.transform.parent.gameObject;

(In other words, the ierarchy is Town->Building->Citizen) And it works almost perfectly fine, but sometimes Location is defined as Town, not as Building. What can be the problem here?

Comment
Add comment · Show 8
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 Andres-Fernandez · May 15, 2015 at 09:57 AM 0
Share

That's too few information here, but it looks like that citizen may not be parented correctly. Try debugging that code to see if that's the case.

avatar image wijesijp · May 15, 2015 at 11:03 AM 0
Share

What do you mean by

Location is defined as Town, not as Building

avatar image UniqueMAX · May 15, 2015 at 01:28 PM 0
Share

I mean that in result variable Location has value Town, not Building as it should be and as it usually is.

avatar image InsertFunnyUsernameHere · May 15, 2015 at 02:00 PM 0
Share

Check out the Hierarchy and see if all the citizens are attached correctly to their parents during runtime. You have 10 citizens and 6 buildings so some buildings will parent multiple citizens. $$anonymous$$ake sure no citizen is a child directly to the town.

avatar image Dblfstr · May 15, 2015 at 07:57 PM 0
Share

Yep, looks to me like evey now and then, a citezen is not parented properly and is assigned to town. How does the game know what to parent the citezen to? $$anonymous$$eaning, is it colliders or triggers, or some other method? Need to see the code.

Show more comments

1 Reply

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

Answer by UniqueMAX · May 16, 2015 at 12:58 PM

I've just found something really strange while debugging. Turns out in this line

 Victims=Location.GetComponentsInChildren<Transform>();

the Victims[0] object is not the first child of Location but the Location itself.

 Victims[0]=Location
 Victims[1]=CitizenChild1
 Victims[2]=CitizenChild2

and etc.

I wanted to ask if the first index in C# arrays is 0 or 1? And does the last index equal Array.Length or Array.Length-1?

Comment
Add comment · Show 3 · 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 Dblfstr · May 16, 2015 at 01:59 PM 0
Share

An array with 6 elements has indexes 0-5.

avatar image Dblfstr · May 16, 2015 at 02:03 PM 0
Share

Also Random.Range excluded the upper bound. $$anonymous$$eaning random.range(0,6) will give 0 1 2 3 4 5.

So Random.Range(0,victims.length); is apprpriate.

avatar image UniqueMAX · May 16, 2015 at 03:10 PM 0
Share

Oh, thank you! I was getting an error in range when there were no citizens in Location.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image

Related Questions

Make a simple tree 1 Answer

accessing all of a colliders parents children 1 Answer

get the OnCollisionEnter or Stay from the parent? 2 Answers

Spawn inside of a parent 1 Answer

Get Child from a parent 3 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