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 Vice_Versa · Jun 24, 2015 at 06:41 PM · nullreferenceexceptionparentchildobjectsinstatiate

how to change the Parent of an object to the current object that a script is on?

so im trying to instantiate a new object, set its parent to be the parent of the objewct that the current script is attached to, then set its rotation to the rotation of the parent object. i keep getting anull reference exception whenever i try to set the parent of the newly instantiated object. What am i doing wrong here?

         Vector3 selectLocation = new Vector3 (transform.position.x, transform.position.y - 2, transform.position.z);
         Vector3 orbitLocation = new Vector3 (transform.position.x + .3f, transform.position.y - 1, transform.position.z - 2f);
 
             temp_select = Instantiate(selectButton, selectLocation, selectButton.rotation)as GameObject; 
         temp_orbit = Instantiate (orbitButton, orbitLocation, orbitButton.rotation)as GameObject;
 
 
             temp_select.transform.parent = this.gameObject.transform;
             temp_orbit.transform.parent = this.gameObject.transform;
 
         temp_select.transform.rotation = selectButton.transform.parent.transform.rotation;
         temp_orbit.transform.rotation = orbitButton.transform.parent.transform.rotation;
     }
 
     public void killButtons()
     {
         Destroy (temp_select);
         Destroy (temp_orbit);
     }
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 Dave-Carlile · Jun 24, 2015 at 06:52 PM 0
Share

On which line does the exception occur?

avatar image Cherno · Jun 24, 2015 at 06:56 PM 0
Share

Do the "temp_select" and "temp_orbit" GameObjects get instantiated correctly? Chances are, one or both of the prefabs ("selectButton", "orbitButton") are null so no copy can be made. Use Debug.Log lines to check if the clones are instantiated correctly.

1 Reply

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

Answer by Flightkick · Jun 24, 2015 at 07:08 PM

You need to use SetParent() instead of setting it directly.

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

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

Does animating an object with children still work? (legacy) 1 Answer

Parented Empty GameObjects sticking to other objects problem 1 Answer

Parenting Transforms, Must (still) Set Parent at 0,0,0 ? 1 Answer

how would I "child" an un-rendered 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