• 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 corax · May 20, 2012 at 02:41 PM · nullreferenceexceptionparent

Parenting problem

Hi guys I'm trying to instantiate an object with a parent to a point of view but i get null reference. Here is the code:

 var showMessage : boolean;
 var someMessage : String;
 var prefab : Transform;
 var pov : GameObject;
 
 private var video : GameObject;
 
 function OnGUI(){
     if(showMessage){
         GUI.Box(Rect(Screen.width/2,Screen.height/2, 100, 25),someMessage);
     }
 }
 
 function OnTriggerEnter(){
     showMessage = true;
     videostart();
 }
 
 function OnTriggerExit(){
     showMessage = false;
 }
 
 function videostart() {
     video = Instantiate(prefab, pov.position, pov.rotation) as GameObject;
     video.transform.parent = pov.transform;
 }
Comment
Add comment · Show 1
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 corax · May 20, 2012 at 03:26 PM 0
Share

Nop because the sphere (the testing prefab) is there when enter the trigger. The problem is that the sphere is not parented to the pov :( POV prefab with a sphere (if I drag the prefab in the scene it works)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by kolban · May 20, 2012 at 03:10 PM

When you get a null reference error, the error will be said to have happened at a particular source line. When posting these questions, always identify which source line in the script code Unity has detected the error upon. Also, have a practice with "code" formatting in the posts.

Assuming that the error occurs at the Instantiate line then either:

  1. prefab is null

  2. pov is null

Check, check and double check that these publicly exported variables have values.

Comment
Add comment · Show 4 · 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 Bunny83 · May 20, 2012 at 03:20 PM 1
Share

I would say this is a valid answer ;) converted!

avatar image Bunny83 · May 20, 2012 at 03:24 PM 0
Share

BTW: just realised that "pov" is a GameObject. A GameObject doesn't have a position or rotation property, only the Transform component. The best thing is to change the type of the pov variable to Transform and reassign the object to the variable.

avatar image Berenger · May 20, 2012 at 03:25 PM 0
Share

Furthermore, please do not write parts of your code that are obviously not relevant. OnTriggerExit and the GUI, for instance.

avatar image Bunny83 · May 20, 2012 at 03:27 PM 0
Share

Argh :D another thing:

prefab is of type Transform and you try to as-cast it to GameObject. This will always fail since a GameObject and a Transform are two totally different things. The as-cast in it's nature will return null when the cast fails, so your "video" variable will always be null. Get your types right. You might want to use pragma strict to avoid such mistakes in the future.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 2 Answers

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

NullRef on my parent script 1 Answer

Make a simple tree 1 Answer

Is it possible to get a cloned gameObject's parent name? 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