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 Jelle van den Heuvel · Feb 11, 2015 at 09:47 AM · arrayindexoutofrangeexception

IndexOutOfRangeException C#

Hey People,

Im pretty new to programming and ran into a problem while working on a script for a inventory system

IndexOutOfRangeException: Array index is out of range. Inventory.Update () (at Assets/_Scripts/Inventory.cs:57)

can anybody spot the problem in my code please?

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : CollisionDetection {
     
     public GameObject[] InventoryArray;
     public int InventoryX;
     public float InventoryIncrease;
     public float[] targetPosition = new float[0];
     public float[] startPosition = new float[0];
     public GameObject inventory;
 
     //GameObject[] prefabArray;
     public GameObject prefab = null;
     public int amountToSpawn; //ARRAY.LENGTH
     static public int amountspawned = 0;
     public int x;
     public int a = 0;
     
     int xCounter = 0;
     int y;
     Vector3 newPos;
 
     public int pos;
     
     void Spawner (){
         for (int i = 0; i < amountToSpawn; i++) {
 
             newPos = transform.position;
             newPos.x = newPos.x + xCounter;
             newPos.y = newPos.y - y;
             
             xCounter = xCounter + 1;
             
             if(xCounter == x){
                 y += 1;
                 xCounter = 0;
             }
             
             (Instantiate (prefab, newPos, transform.rotation)as GameObject).transform.parent = inventory.transform;
             amountspawned ++;
         }
     }
 
     // Update is called once per frame
     void Update () {
     
         if (InventoryArray.Length >= amountspawned) {
             Spawner ();
         }
 
 
         InventoryArray = GameObject.FindGameObjectsWithTag("ItemInInventory");
         for ( int i=0; i < InventoryArray.Length; i++) {    
             print (InventoryArray [0].transform.position);
             InventoryArray[i].transform.localScale = new Vector3 (.7f,.7f,.7f);
             InventoryArray[a].transform.position = new Vector3(newPos.x,newPos.y,(inventory.transform.position.z)-.5f);
             GameObject.FindGameObjectWithTag("ItemInInventory").transform.parent = inventory.transform;
             a++;
             inventory.SetActive(false);
             inventory.SetActive(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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Joyrider · Feb 11, 2015 at 09:49 AM

As the debug info tells you, the error is at line 57, and you have an index that is bigger than the highest value of your array.

My guess is you changed the value of `a` in the editor to a value bigger or equal to InventoryArray.Length

Comment
Add comment · Show 2 · 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 Jelle van den Heuvel · Feb 11, 2015 at 11:10 AM 0
Share

doesnt seem to work

avatar image Jelle van den Heuvel · Feb 11, 2015 at 11:14 AM 0
Share

the whole script seems to work just fine, but i keep getting the error. I can actually play further with the error

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problem with Singleton and NullReferenceException? 0 Answers

Simple Looping drag and drop game 1 Answer

Array index is out of range error Edited 1 Answer

FindGameobjectsWithTag -> Array 2 Answers

IndexOutOfRangeException: Array index is out of range. 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