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 Unused Mass · May 31, 2013 at 06:31 PM · javascriptgameobjectarray

Array with Gameobjects code

I've been trying to solve this thing for about an hour now, could somebody tell me why this error:

 NullReferenceException: Object reference not set to an instance of an object
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
 Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
 UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
 GUI [ Manager].Update () (at Assets/GUI [ Manager].js:15)

is happening with this code? :

 var timer: float = 300; // set duration time in seconds in the Inspector
 var cube: GameObject;
 var cubeArray;
 function Start() {
     var cubeArray = new Array();
 }
 function Update(){
   timer -= Time.deltaTime;
   if (timer < 0){
      timer = 5;
      cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
      cube.AddComponent("Cube");
      cube.transform.position = Vector3 (0, 30, 0);
      
      cubeArray.Add( cube );
      
        for (var i = 0; i < cubeArray.Length; i++)  { 
              cubeArray[i].Fall();
             }
   } else {
         guiText.text = timer.ToString("F1");
     }
   }



"Cube" is a script, if anyone is wondering. Thanks.

Comment
Add comment · Show 3
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 robertbu · May 31, 2013 at 06:32 PM 0
Share

In Start() you have 'var cubeArray' so you are initializing a local variable rather than the top-level cubeArray. Remove the 'var'.

avatar image Unused Mass · May 31, 2013 at 06:35 PM 0
Share

Done, but it still did not fix my problem.

avatar image robertbu · May 31, 2013 at 06:58 PM 0
Share

Sure didn't. I converted my answer into a comment. I don't use the Array class, and unfortunately I don't have time now to look into it.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · May 31, 2013 at 08:19 PM

Don't use the Array class. Always use built-in arrays or generic Lists instead. The only things Array has by comparison are casting problems, slower code, and fewer features. So forget it exists. Also never do things like var cubeArray;. You must always define the type, either explicitly, or implicitly by supplying a value.

Comment
Add comment · Show 3 · 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 Unused Mass · Jun 02, 2013 at 08:43 PM 0
Share

I have changed my code, but it still gives the error: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) GUI [ $$anonymous$$anager].Update () (at Assets/GUI [ $$anonymous$$anager].js:16)

with the code: import System.Collections.Generic;

 var timer: float = 300; // set duration time in seconds in the Inspector
 var cubeArray;
 function Start() {
      var cubeArray = new List.<GameObject>();
 }
 function Update(){
   timer -= Time.deltaTime;
   if (timer < 0){
      timer = 5;
      cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
      cube.AddComponent("Cube");
      cube.transform.position = Vector3 (0, 30, 0);
      
      cubeArray.Add(cube);
      
        for (selectedCube in ArrayList)  { 
              
             }
   } else {
         guiText.text = timer.ToString("F1");
     }
   }
 
avatar image Eric5h5 · Jun 02, 2013 at 08:59 PM 0
Share

You haven't initialized cubeArray. You created a new local cubeArray in Start, but you never use it for anything. Also, like I said, you can't use "var cubeArray"; you must always declare the type explicitly or implicitly. Additionally, I don't know what "`for (selectedCube in ArrayList)`" is supposed to be, but it wouldn't work since ArrayList is a type. While "`cube.AddComponent("Cube");`" would technically work, you should generally not use the string version.

avatar image Unused Mass · Jun 03, 2013 at 02:10 PM 0
Share

Thank you, that answered my question and my code is now working.

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

15 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

Related Questions

NullReferenceException with object array 1 Answer

Can't destroy Game Object in Array 2 Answers

Access a String array in one script from another script 0 Answers

Index out of bounds even tho it shouldnt be [Javascript] 1 Answer

How do I create an array for multiple targets? 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