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 Vaughanjb · Apr 09, 2014 at 12:45 PM · javascriptinstantiatearraysclone

Cannot add Instaniated gameObjects to a GameObject[] using Javascript. Error: "NullReferenceException: Object reference not set to an instance of an object"

I have a script that is Instantiating gameobjects but when I assign them to a variable I cannot add that gameObject to a GameObject array so I can use it later.

 for ( var i = 0; i < amount; i++ ) {
             
         var object_id : int = Random.Range(0,platform_array.length);
         
         var platform_clone = Instantiate(platform_array[ object_id ], Vector3(0, 0, platform_length*platform_count) , Quaternion()); // The Instantiate command takes a GameObject, a Vector3 for position and a Quaternion for rotation.
         
         Debug.Log( platform_clone );
         
         platform_clone_array.Add( platform_clone.gameObject );
         
         platform_count++;
             
     }


I have tried using:

 platform_clone_array.Add( platform_clone.gameObject );
 
 platform_clone_array.Add( platform_clone );
 
 platform_clone_array.Push( platform_clone);
 
 platform_clone_array += platform_clone;
 
 platform_clone_array[i] = platform_clone.gameObject;


Which no success. I can Debug.Log it and it appears "brick_checkpoint(Clone) (UnityEngine.GameObject)" exactly as it should and also in the heirachy but just says "Object reference not set to an instance of an object" when I try and add it to the array.

Any help on this would be great, as I am pretty new to Unity but have experience with other code.

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 KiraSensei · Apr 09, 2014 at 12:44 PM 0
Share

I don't see the code where you define platform_clone_array, you need to create this var before using it.

avatar image Vaughanjb · Apr 09, 2014 at 02:01 PM 0
Share

Sorry,

It was defined at the top of my script as

 private var platform_clone_array : GameObject[];

but this was actually where the error lied. I will post the answer now.

2 Replies

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

Answer by Vaughanjb · Apr 09, 2014 at 02:43 PM

This has now been fixed.

I was using

 private var platform_clone_array : GameObject[];

Which you cannot add or remove from so instead I am now using

 private var platform_clone_array = new Array();


Then to add to the array:

 platform_clone_array.push (gameObject);
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
0

Answer by thinkwayne · Apr 09, 2014 at 03:27 PM

My Javascript is a little rusty but as KiraSensei said in the comments you need to define your array:

 var platform_clone_array = new Array();
 
 //your code
 for ( var i = 0; i < amount; i++ ) {
  
        var object_id : int = Random.Range(0,platform_array.length);
  
         var platform_clone = Instantiate(platform_array[ object_id ], Vector3(0, 0, platform_length*platform_count) , Quaternion()); // The Instantiate command takes a GameObject, a Vector3 for position and a Quaternion for rotation.
  
        Debug.Log( platform_clone );


        platform_clone_array[i] = platform_clone.gameObject;
  
        platform_count++;
  
     }
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

Instantiating a random dropped consumable item from many cloned objects 1 Answer

Instantiate without (clone) in JS. 1 Answer

Accessing variable of instantiated prefab inside array in javascript 1 Answer

Following Object can't find newly Instantiated Object (Solved) 1 Answer

How to spawn objects from CSV file 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