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 Simon Crowe · May 01, 2011 at 06:13 PM · javascriptnullreferenceexception

"NullReferenceException" Error when trying to count objects with a particular tag using GameObject.FindGameObjectsWithTag function

Hello. I have just started coding in UnityScript roughly a week ago and have no prior coding experience, so don't be surprised if this query seems ignorant.

I want to instruct unity to spawn a number of cubes at unique points on a grid. I don't want two or more cubes to occupy the same space, nor do I want to end up with any less than the my desired number (value of the GlobalVar.numberCubeObstacles variable.)

The countCubes function should be invoked once for every iteration of the while loop, making it possible to derive the number of objects with the "CubeObstacle" tag from the length of the CubeCount variable (an array of references to GameObjects?).This should cause it repeat the instructions as many times as necessary.

When I run the game, the code compiles normally but I get this error:

NullReferenceException: Object reference not set to an instance of an object UnityScript.Lang.Extensions.get_length (System.Array a) Spawn.Start () (at Assets/Evolution Simulation/Scripts/Spawn.js:10)

Here is the relevant code including the CubeCount function:

function Start(){ var cubeCount : GameObject[]; var CollidersCheck; Invoke ("countCubes",0);

 while (cubeCount.length<=GlobalVar.numberCubeObstacles){//<< This is line 10
     Invoke ("countCubes",0);
     var cubePosition = Vector3(Random.Range(-18, 18),0.5,Random.Range(-18, 18));
     CollidersCheck = Physics.OverlapSphere (cubePosition,0.5);
         if (CollidersCheck.lenght == 0){
         //No colliders detected
         Instantiate(cubePrefab, cubePosition, Quaternion.identity);
         }
 } 

}

function countCubes(){ cubeCount = GameObject.FindGameObjectsWithTag("CubeObstacle"); }

I did something similar earlier, simply printing the length of an array of game objects with a particular tag to the console and it worked. I'm not sure if the problem is specifically how I'm attempting to use the length of the array or some other mistake.

Comment
Add comment
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

1 Reply

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

Answer by Simon Crowe · May 01, 2011 at 06:47 PM

I am fairly certain of what was wrong with my code. Initially, the CubeCount Array would be null due to the lack of any objects in the scene tagged CubeObstacle. This would mean that any attempt to reference it would result in the "NullReferenceException: Object reference not set to an instance of an object" error.

Comment
Add comment · Show 1 · 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 Mike 3 · May 01, 2011 at 07:02 PM 0
Share

the main issue is that you're calling countCubes a frame later than using it (Invoke with 0 won't call it immediately). use countCubes() ins$$anonymous$$d of invoke and it should fix it

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

No one has followed this question yet.

Related Questions

Split() into a fixed length array? 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

NullReferenceException in follow function 0 Answers

How can I stop getting Null Reference Exception? 1 Answer

Instanciate Object Problem for Shooting 2 Answers


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