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 ladygabe · Nov 23, 2013 at 07:38 AM · arraybeginnernullreferenceexceptionarray of gameobjects

NullException Error With Array of GameObjects

I am trying to create an array of gameObjects, which is then referenced by a function that turns on the renderer and collider of each when called. However, when I playtest, occasionally it will work for one of the cylinders, and the rest of the time it throws a NullReferenceException. I was wondering if anyone can see what I did wrong? I've looked through other similar questions and still cannot figure out why it is doing this.

Here is the instantiation (for reference, there are four cylinders/rings in the scene).

 private var Rings : GameObject[] = new GameObject[10]; //array of rings
 
 function Start () {
 //count rings
      totalAmount++; //totalamount = totalamount +1;
      Debug.Log("Total Amount of Rings = " + totalAmount);
  
         Rings[totalAmount] = GameObject.Find("Cylinder." + totalAmount);
         Debug.Log("Found game object: " + totalAmount);
 
 }

And here is the function where it is called.

 function TurnOnAllRenderers(){ //turn all ring renderers back on if the player is reset to beginning
     for(var i = 1; i <= totalAmount; i++)
     {
     Debug.Log("turn on = " + i); 
      Rings[i].renderer.enabled = true; 
      Rings[i].collider.enabled = true; 
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Nov 23, 2013 at 07:42 AM

Your array only stores one item in the Start at the index 1 so your loop in the method does not find anything.

EDIT: based on your comment, the error is different. So I would guess totalAmount is static since it increases as expected. But your array is private You actually have an array for each script.

So the first script1 places object1 in slot 1 of array1 using totalAmount, but script2 places object2 in slot 2 array2 using totalAmount.

Solutions:

Easy: make the array static.

More complex: Store the array in some Manager script and use GetComponent and find the array:

 var manager :Manager;

 function Start(){
     manager = GameObject.Find("Manager").GetComponent(Manager);
     manager.array[totalAmount] = object;
 }

Also, try to start from 0 as you may get out of bound.

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 ladygabe · Nov 23, 2013 at 07:05 PM 0
Share

There are multiple instances of the script, so totalAmount rises to four by the end of the instantiation of the objects (since each has the script). The debugs in the first return:

Total Amount of Rings = 1 Found game object: 1

Total Amount of Rings = 2 Found game object: 2

Total Amount of Rings = 3 Found game object: 3

Total Amount of Rings = 4 Found game object: 4

avatar image
0

Answer by V4mpy · Nov 23, 2013 at 09:03 AM

Use update for increasing the total amount - or use a loop to do this. Also start a loop at index 0.

Your totalAmount is 1. So your loop goes from 1 to 1. -> the loop is doing nothing

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 ladygabe · Nov 23, 2013 at 07:05 PM 0
Share

There are multiple instances of the script, so totalAmount rises to four by the end of the instantiation of the objects (since each has the script). The debugs in the first return:

Total Amount of Rings = 1 Found game object: 1

Total Amount of Rings = 2 Found game object: 2

Total Amount of Rings = 3 Found game object: 3

Total Amount of Rings = 4 Found game object: 4

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

17 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

Related Questions

I want to add cloned gameobjects that are triggered, to an array. 2 Answers

NullReferenceException with Javascript Array 2 Answers

Array of custom properties? (C#) 1 Answer

How to get values from JSON and makes array of GameObject 1 Answer

Draw a line from gameobject at end of the array to the gameobject in the start of the array 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