• 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 Jondea · Aug 11, 2013 at 10:58 AM · instantiate prefab

NullReferenceException inside clone script

I keep getting the same error on this and it's had me stumped for hours. Essentially I instantiate an object called "unit", which has a script called movement which instantiates "head" (in the start function). Except that it doesn't instantiate it, the following code gives this.

 public class Movement : MonoBehaviour { //Movement is the behaviour which allows a unit to move
     
     public Transform HeadPrefab;
     public Transform Block;
     public int MaxSize = 5;
     
     Transform head;
     List<Transform> blockList = new List<Transform>();
     
     void Start () {
         
         Vector3 headPosition = new Vector3(0,5,-11);    
         
         head = Instantiate (HeadPrefab, headPosition, Quaternion.identity) as Transform;
         Debug.Log(head.transform.position.x);
     }
 }

gives: NullReferenceException: Object reference not set to an instance of an object. at runtime, when compiled there are no errors.

The unit object is instantiated by a script called GameArena and is as follows

     public Transform unit;
     public Transform enemy;
     
     Transform clone;
     
     void Start () {
         
         Vector3 unitPosition = new Vector3(0,5,-11);
         Vector3 enemyPosition = new Vector3(10,5,-11);        
         
         clone = Instantiate (unit, unitPosition, Quaternion.identity) as Transform;
         Instantiate (enemy, enemyPosition, Quaternion.identity);
         Debug.Log(clone.transform.position.x);
         Debug.Log(clone);
     }

again no errors at all on this and it reports to the console the correct position of 0.

I'm confused that I seem to be doing the same things twice but it only works one time, what's different in "Movement"?

Comment
Add comment · Show 4
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 fafase · Aug 11, 2013 at 11:03 AM 0
Share

I guess you have an object in your inspector for HeadPrefab.

Also, try with this:

 head = (Transform)Instantiate (HeadPrefab, headPosition, Quaternion.identity);

it won't fix it but this time if the cast is not possible, you get an error. So you know your issue is in HeadPrefab.

Finally you can try and see if that goes better:

 GameObject head;
 GameObject HeadPrefab;
 
 head = (GameObject)Instantiate (HeadPrefab, headPosition, Quaternion.identity);
avatar image Benproductions1 · Aug 11, 2013 at 11:24 PM 0
Share

Where is your null reference exception?

avatar image Jondea · Aug 13, 2013 at 06:31 PM 0
Share

Line 15 on the first bit of code, as in it gets past the instantiate, it even makes the object visible on the scene and game view. However, it doesn't actually save it into the variable "head" and therefore my command "head.transform.position.x" is the null reference.

avatar image Jondea · Aug 13, 2013 at 09:02 PM 0
Share

fafase it worked, many thanks. Just curious but why is it that one prefab was fine being instantiated as a Transform and the other had to to be a GameObject?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IgorAherne · Aug 13, 2013 at 09:10 PM

I've created a sphere prefab and plugged it into the first script in inspector for both transform variables. Everything is working without errors. Check your prefabs

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.

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

16 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

Related Questions

unity gameObject set id before instantiation 3 Answers

Having problem with Instatiate GameObject at Canvas 0 Answers

Im trying to instantiate prefab from an array but get a confusing response...(this prefab will be consumed by the player meaning it needs to be instantiated multiple times) 2 Answers

Prefab attached to script is destroying itself before Instantiate() is called 1 Answer

Spawn a prefab off screen? 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