Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by skyeyemachine · Oct 22, 2012 at 12:07 PM · c#gameobjectarrayparentchild

Can one specify the parent of a gameobject in an array?

Hello all. I am trying to update the parent-child relationships of some gameobjects that are contained in a GameObject[] fixed-length array (size 3). Basically, I have three simple gameobjects (no physics data, just mesh rendering, etc) loaded from Prefabs in my scene, each of which has a child GameObject that I want to assign as a child to one of three other, empty gameobjects, which act as position markers in my scene. My current approach to this is seen in "initializeVonSouffleQueue()" below.

Basically, however, I am having troubles with NullReferenceException errors, mostly coming from line 6 from the top of "updateVonSouffleQueue()" as below. I'm not quite sure how to fix things, however. At its simplest, I'm trying to do what is seen in this link, only with gameobjects contained inside arrays rather than single variables: Parenting and Re-Parenting

Any insight into how to better approach the matters of dynamic reassignment of array members and gameobject parent-child relationships in script would be helpful. Thanks.

 void initializeVonSouffleQueue() {
         for (int i = 0; i < souffleQueue.Length; i++)
         {
             souffleQueue[i] = Instantiate((scriptCamera.ingredientTypeItem[0][Random.Range(0, numNeutralIngredient)] as GameObject).transform.FindChild("prefabIngredientFace"), queueGUI[i].transform.position, Quaternion.identity) as GameObject;
             souffleQueue[i].transform.parent = queueGUI[i].transform;
         }       
 
     }
 
 
  public void updateVonSouffleQueue() {
     for (int i = 0; i < souffleQueue.Length-1; i++) 
     {
        souffleQueue[i] = souffleQueue[i+1];
        souffleQueue[i].transform.parent = queueGUI[i].transform;
     }
     souffleQueue[2] = Instantiate((scriptCamera.ingredientTypeItem[0][Random.Range(0, numNeutralIngredient)] as GameObject).transform.FindChild("prefabIngredientFace"), queueGUI[2].transform.position, Quaternion.identity) as GameObject;
  }

[1]: http://wiki.etc.cmu.edu/unity3d/index.php/Parenting_&_Re-Parenting

Comment
Add comment · Show 7
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 Loius · Oct 22, 2012 at 03:58 PM 1
Share

arrays are fine, just don't use Arrays. With a capital A.

avatar image DaveA · Oct 22, 2012 at 09:51 PM 0
Share

Lines this long are hard to parse, I would break that into at least two different lines:

souffleQueue[i] = Instantiate((scriptCamera.ingredientTypeItem[0][Random.Range(0, numNeutralIngredient)] as GameObject).transform.FindChild("prefabIngredientFace"), queueGUI[i].transform.position, Quaternion.identity) as GameObject;

avatar image asafsitner · Oct 22, 2012 at 11:33 PM 0
Share

@Fattie:

I'd very much like to hear your reasoning for that.

Unless you meant Arrays, as Louis said.

avatar image hvilela · Oct 23, 2012 at 12:04 AM 0
Share

Why people give answers as comment... I can't understand that.

avatar image Xroft666 · Oct 23, 2012 at 12:06 AM 0
Share

Becouse a normal answer must be a solid one. Coments are uncertain :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skyeyemachine · Oct 27, 2012 at 08:26 AM

Hello all. I'm replying to confirm that I've since resolved my problem.

There were a few things wrong in general, and I wound up refactoring my code several times before I was done. The key points though where that 1) I wasn't keeping close enough track of what types my function calls were returning, 2) I didn't fully understand how to interact with components and GameObjects effectively in script, and 3) I was attempting to alter the parent of a prefab object directly, rather than instantiating a clone and altering that instead.

To anyone who encounters similar issues, I suggest you take a close look at exactly what types you are working with in your scripts, particularly when it comes to working with built-in arrays (not javascript Arrays). I was particularly thrown by conversions between Transforms, Objects, and GameObjects; double-check this in the Unity official documentation. Also, ensure your arrays are actually being loaded with values properly; I kept getting Null Exceptions simply because my arrays kept winding up empty for one reason or another.

Hope this helps. Thanks to all who replied.

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 Fattie · Oct 27, 2012 at 08:35 AM 0
Share

i suggest unityGE$$anonymous$$S.com which has an outstanding beginner article on using GetComponent, and that whole aspect of of program$$anonymous$$g

regarding tracking items in a pool.

you are correct that this is an absolutely fundamental part of engineering video games. ($$anonymous$$y guess, one day Unity will include the concept as a foundation.)

Here is a novel-length chitty chat about the topic !

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

hope it helps

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

Accessing variable from parent 1 Answer

Make a simple tree 1 Answer

"Center On Children" programmatically 1 Answer

Instantiated GameObject gets spawned as a child 2 Answers

Moving a GameObject reffering to the coordinates of the parent Object 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