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 RayJr · Feb 02, 2015 at 01:02 AM · transformprefabchild

Prefab as child of existing GameObject

I've seen this asked 50 times and the common answer isn't working for me. Any ideas? here is my code, i want to create a prefab as a child of the current object

 using UnityEngine;
 using System.Collections;
 
 public class Scheduler : MonoBehaviour {
     public Transform X;

     void Start () {
     
     }
 
 
 void OnMouseDown (){
 
     switch (this.name){
     case "d113":
             GameObject test = Instantiate(X, new Vector3 (0,0,0), Quaternion.identity) as GameObject; 
             test.transform.parent = transform; 
         break;
         
         }
 
 
     }
 
 
 }
 

And I always get this error against the second line: NullReferenceException: Object reference not set to an instance of an object

Seems pretty simple, I'm not sure what I could be missing. If i take out the second line, it works find and the prefab is created without a parent.

Thanks!

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 Simon-Larsen · Feb 02, 2015 at 01:16 AM 0
Share

That should work. Something else may be going wrong, not sure what though. However the quaternion should be with a capital Q.

avatar image RayJr · Feb 02, 2015 at 01:36 AM 0
Share

Yup, Same effect.

I also tried changing the second line to add a regular GameObject and it works.

Updating to include entire script...

2 Replies

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

Answer by RayJr · Feb 02, 2015 at 01:53 AM

Oops, nevermind. Was declaring it as a Transform in line 5. I must have combined solutions and screw it all up. Thanks !

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

Answer by Simon-Larsen · Feb 02, 2015 at 01:59 AM

Your problem is that X is a Transform, but you're trying to cast it to a GameObject, however, Tranform doesn't inherit from GameObject so that won't work. You could either cast your instantiation result to a Transform or you could change your X variable type to GameObject.

So, either

 Transformm test = Instantiate(X, new Vector3 (0,0,0), Quaternion.identity) as Transformm; 
 test.parent = transform; 

or

 public GameObject X;

Remember to reassign your prefab to your script if you're changing the type.

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

20 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

Related Questions

Prefab Transform & Light Child Transform 1 Answer

setting the parent of a transform which resdes in a prefab is dusabled to prevent data corruption 1 Answer

Instantiate a Prefab as child 0 Answers

Spawn Shield to all objects 2 Answers

Make a simple tree 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