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
1
Question by Pascal1234 · Apr 26, 2013 at 03:49 PM · errorinstantiateparenting

Instantiate Object and set its parent throws Null Reference Error

Hey guys, i have read some answeres about that but no one works for me. I tried to create several objects and set their parent. But it seems like the objects are not created when i try to set the transform.parent value This is my code:

 void OnWizardCreate()
         {
             GameObject parentObj = new GameObject();
             parentObj.name = roomName;
             if (!parentObj)
             {
                 Debug.Log("parentObj is not existing yet");
             }
             else
             {
                 for (x = 0; x < roomWidthX; x++)
                 {
                     for (z = 0; z < roomWidthZ; z++)
                     {
                         GameObject temp = GameObject.Instantiate(tile, new Vector3(x + startPosition.x, startPosition.y, z + startPosition.z), Quaternion.identity) as GameObject;
                         if (temp == null)
                         {
                             Debug.Log("temp is not existing yet");
                         }
                         else
                         {
                             temp.transform.parent = parentObj.transform;
                         }
                     }
                 }
     }

when i start this function the console throws that the temp Object is not yet existing. If i do that without the if(temp == null) part then i get a Null Reference exception. Any ideas on what could solve my problems are very welcome!

Comment
Add comment · Show 2
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 robertbu · Apr 26, 2013 at 03:52 PM 0
Share

How is 'tile' defined and initialized?

avatar image Pascal1234 · Apr 28, 2013 at 06:13 PM 0
Share

public Transform tile; i set it in unity and its a normal gameobject

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by whydoidoit · Apr 28, 2013 at 06:14 PM

So your problem is that a Transform is not a GameObject. When you instantiate a Transform, you get a Transform - doing As GameObject returns null because it isn't a GameObject.

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 Loius · Apr 28, 2013 at 06:45 PM 1
Share

and you can retrieve its GameObject using itsTransform.gameObject

avatar image EliteMossy · Apr 28, 2013 at 11:26 PM 0
Share

I assumed it was a GameObject, given the original limited question.

avatar image
0

Answer by EliteMossy · Apr 26, 2013 at 03:55 PM

Firstly you should be checking for null BEFORE setting the name

  parentObj.name = roomName;
             if (!parentObj)
             {
                 Debug.Log("parentObj is not existing yet");
             }

is wrong, parentObj.name = roomName; should be in the else. Otherwise would be kind of pointless.

Also i am assuming tile is null, so do this test after void OnWizardCreate() {

 Debug.Log(tile == null ? "Tile is null" : "Tile is ok!");

Only reason i can think off.

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 Pascal1234 · Apr 28, 2013 at 06:13 PM 0
Share

$$anonymous$$y problem is not the parent object, it says that the temp object is null, so after instantiate this object i cannot set its parent because it does not exist in this moment, maybe it would work if i wait for some time. But i saw so much others who do it like that and no one gets errors

avatar image Pascal1234 · Apr 28, 2013 at 06:14 PM 0
Share

The parentObj is not theh problem, it says that the temp object does not exist in this moment. $$anonymous$$aybe it works if i wait for some time but i saw so many others who do it like me in the code above and they do not get errors.

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

Simultaneous Null Reference Exception and expected value 1 Answer

[Solved] How to Dynamicaly assemble an Object in your Resource Folder 1 Answer

updateMassFromShapes error 2 Answers

Prefab changing dimensions when parent changed 1 Answer

Keep Instantiated GameObject while exit gamemode in unity 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