Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one button that every press activates a structure and at the last structure the button became deactivated
but when I get to the 4# click(structure range 0-4) I get this error in Unity

"ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection"

this is my code
C#
<pre>using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VillageStructure : MonoBehaviour
{
    public List<GameObject> structureLevels;
    public GameObject buildButtonEnabled;
    public GameObject buildButtonDisabled;
    public GameObject buildEffect;

    int currentLevel;


    // Start is called before the first frame update
    void Start()
    {
        currentLevel = 0;
    }

    // Update is called once per frame
    void Update()
    {
        if (currentLevel >= structureLevels.Count || currentLevel < 0)
        {
            buildButtonEnabled.SetActive(false);
            buildButtonDisabled.SetActive(true);
        }
    }

    public void BuildStructure()
    {
        if (currentLevel < structureLevels.Count)
        {
            buildButtonEnabled.SetActive(true);
            buildButtonDisabled.SetActive(false);

            if (currentLevel > 0)
            {
                structureLevels[currentLevel - 1].SetActive(false);
            }

            structureLevels[currentLevel].SetActive(true);

            // If you have an Animator component on the current structure level
            Animator anim = structureLevels[currentLevel].GetComponent<Animator>();
            if (anim != null)
            {
                // Play animation or do other animation-related actions
            }

            currentLevel++;
        }
        else
        {
            buildButtonEnabled.SetActive(false);
            buildButtonDisabled.SetActive(true);
        }
    }
}


What's wrong? :\

What I have tried:

tryied to edit the update section with

if (currentLevel >= structureLevels.Count || currentLevel < 0 || currentLevel > 4)
{
    buildButtonEnabled.SetActive(false);
    buildButtonDisabled.SetActive(true);
}


but no success
Posted
Updated 2-Jan-24 8:11am
v5
Comments
Mike Hankey 31-Dec-23 15:36pm    
Have you run it through the debugger and put a break at the offending line and see how many values are in structureLevels?
Dave Kreskowiak 31-Dec-23 18:11pm    
So set a breakpoint on the first line of this behavior code and run the app. When the app stops at the breakpoint, step through the code line-by-line and hover the mouse over variable names to see their contents. The yellow highlight is the current line that is going to be executed next when you hit F10 or F11.
[no name] 1-Jan-24 17:33pm    
You're going to be in for a lot of pain unless you come up with some naming standards.

https://www.codeproject.com/Tips/261001/Csharp-Tips-Tricks#:~:text=C%23%20-%20Naming%20Standards%201%20Coding%20Standards%20for,Last%20in%20a%20Linked%20List%20...%20More%20items
Dave Kreskowiak 2-Jan-24 14:58pm    
Replacing all of your question text with garbage after it has been answered is considered very rude, not just on CP but all other sites as well. I have rolled the question back to its original v1 content so answers actually make sense.

1 solution

You get an index out of range error when you try to access an array element which doesn't exist.
In C#, arrays are zero based, so an array of N elements with have valid indexes between 0 and (N - 1) only: an array of three elements such as this:
C#
int[] data = new int[] {1, 2, 3};
Wil have valid indexes of 0, 1, and 2 only. And index which is negative or greater than 2 will give you the error.
Since a
List<T>
is built on an array of T values, this applies to them as well.

Most likely, it's this line that throws the exception:
C#
structureLevels[currentLevel].SetActive(true);
Because although you check for an empty list above it, you execute the line of code anyway.

Put a breakpoint on that line, and look at the value of currentLevel and the content of structureLevels to see what exactly you are dealing with: either currentLevel is too large, or structureLevels is empty. Since you do not assign a new List<GameObject> to structureLevels in that code we can have no idea how many elements it might contain!
 
Share this answer
 

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