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 Mychal · Aug 31, 2014 at 03:20 PM · null

NullReferenceException when assigning a parent to an instatiated object.

The game level is made of instatiated tiles and I need to make them a child of a single parent LevelDataParent but I always get a null reference exception.

 void Start(){
         position = new Vector2 (0, 0);
         for (float i = 0; i<ySize; i++){
             for (float j = 0; j<xSize; j++){
                 position.x = j;
                 position.y = i;
                 GameObject clonedTile = Instantiate(tilePrefab[Random.Range(0,tilePrefab.Length)], CartToIso(position), Quaternion.identity) as GameObject;
                 clonedTile.transform.parent = GameObject.Find("LevelDataParent").transform; //Exception is raised here
             }
         }
     }


Thank you in advance.

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
0

Answer by Jessespike · Aug 31, 2014 at 03:56 PM

Is LevelDataParent the name of a GameObject, or a script?

GameObject.Find Finds a game object by name.

GameObject.FindObjectOfType Returns the first active loaded object of the Type.

Try this out:

 clonedTile.transform.parent = GameObject.FindObjectOfType(typeof(LevelDataParent)).transform;


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 BMayne · Aug 31, 2014 at 04:09 PM 0
Share

To expand on jessespike I would suggest using FindGameObjectByTag ins$$anonymous$$d of type or name. All three of these are pretty slow and can but Tag is optimized to be a bit faster. Even better would be to make a private static variable to store the parent ins$$anonymous$$d of finding it every time.

 private static Transform m_parent = null;
 private const string TRANSFOR$$anonymous$$_PARENT_NA$$anonymous$$E = "LevelDataParent";
 public static Transform parent 
 {
    get
    {
       if( m_parent == null )
       {
          m_parent = GameObject.FindGameObjectWithTag(TRANSFOR$$anonymous$$_PARENT_NA$$anonymous$$E).transform;
       }
       return m_parent;
    }
 }
 

Then to set the parent you go

 clonedTile.transform.SetParent(parent, false);

  

I might have gone to far with using static but I am all about making things fast :)

avatar image Mychal · Sep 01, 2014 at 09:43 PM 0
Share

Thanks for response. @Jessespike LevelDataParent is a name of a GameObejct. @b$$anonymous$$ayne .SetParent is not a native unity method. Do I need a plugin or library to call it?

avatar image BMayne · Sep 01, 2014 at 10:01 PM 0
Share

Damn, I think it's only 4.6 that is was added. I have been working with 4.6 for a few months. $$anonymous$$y bad. You can just use

 clonedTile.transform.parent

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animation Error "Null reference exception"? 2 Answers

c# 2D array error: need Help! 1 Answer

Resources.Load texture problem 1 Answer

Fresh installed , with problems 0 Answers

Passing information error 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