Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Trouble!!! Help
index was outside the bounds of the array cs:33

What I have tried:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ComodScript : MonoBehaviour
{
   public GameObject[] Comods;
   int index;

   void Start()
   {
    index = 0;
   }

   void Update()
   {
    if(index > 3)
    index = 0;
    
    if(index==0)
    {
        Comods[0].gameObject.SetActive(true);
    } 
   }
   public void ButtonNext()
   {
    index += 1;

    for(int i = 0; i < 4; i++)
    {
        Comods[i].gameObject.SetActive(false);
        Comods[index].gameObject.SetActive(true);
    }
   }
   
}
Posted
Updated 31-Aug-22 10:39am
v2
Comments
PIEBALDconsult 31-Aug-22 16:23pm    
I expect the value of index becomes 4. Would you like it to wrap back to 0 ?
m0r yana 31-Aug-22 16:35pm    
yes, i am trying to change ui images with button in a circle
PIEBALDconsult 31-Aug-22 16:51pm    
So you need to reset back to 0 when you hit 4.
index = ( index + 1 ) % 4
m0r yana 31-Aug-22 17:00pm    
Thank you so much! it works! <3
PIEBALDconsult 31-Aug-22 17:03pm    
Great. Also, rather than hard-coding 4, use Comods.Length

The error message means exactly what its says: you have tried to access an element of a collection that doesn't exist because the index is invalid.
In C#, indexes run from 0 to (N - 1), when N in the number of elements in the collection.
So for an array of three elements, the valid indexes would be 0, 1, and 2 only - any other value (positive or negative) will give you this error.

But we can't tell you why your index is invalid: we have no access to your data or the values in the index(es) you are using.

So it's going to be up to you.
Fortunately, you has the debugger to help you find out what is going on. Run your code in the debugger, and when the error occurs, it will stop so you can look at your variables and see what they contain.
Then you can start looking for why they are out of bounds.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
m0r yana 31-Aug-22 16:29pm    
i know that, i have 4 elements: 0,1,2,3. everything is correct i dont understand why i have this problem
OriginalGriff 31-Aug-22 16:36pm    
That's why you use the debugger: to find out which of your preconceptions is wrong.
Either you have less than four elements, or you have an index that is not 0, 1, 2, or 4

Your assumptions do not match the error you are getting, so you need information on what exactly is happening - that's what a debugger is for.
Quote:
public void ButtonNext()
{
index += 1; //<- here index might become 4

for(int i = 0; i < 4; i++)
{
Comods[i].gameObject.SetActive(false);
Comods[index].gameObject.SetActive(true);//<- here index = 4 is a problem
}
}
 
Share this answer
 
Comments
m0r yana 31-Aug-22 16:49pm    
i have 4 elements in array
CPallini 1-Sep-22 1:18am    
Indeed, you have items: 0,1,2,3.
You have NOT item 4.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900