I have a problem

Hello, I have a problem, I am Spanish so some names are in Spanish. my error is: IndexOutOfRangeException: Index was outside the bounds of the array. ChangeCharacter.Start () (at Assets / Scripts / ChangeCharacter.cs: 25):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CambiarPersonaje : MonoBehaviour
{
private GameObject[ ] cambiarPersonaje;
private int index;
// Start is called before the first frame update
void Start()
{
index = PlayerPrefs.GetInt(“P”);
cambiarPersonaje = new GameObject[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
cambiarPersonaje = transform.GetChild(i).gameObject;
foreach (GameObject objX in cambiarPersonaje)
objX.SetActive(false);
if (cambiarPersonaje[index])
cambiarPersonaje[index].SetActive(true);
}
public void btnLeft()
{
cambiarPersonaje[index].SetActive(false);
index–;
if (index < 0)
index = cambiarPersonaje.Length - 1;
cambiarPersonaje[index].SetActive(true);
}
public void btnRight()
{
cambiarPersonaje[index].SetActive(false);
index++;
if (index == cambiarPersonaje.Length)
index = 0;
cambiarPersonaje[index].SetActive(true);
}
public void EscenaN()
{
PlayerPrefs.SetInt(“P”, index);
SceneManager.LoadScene(“Escena1”);
}
}

Which line is line 25? Is it this line?
* *if (cambiarPersonaje[index])* *

If so, you’re just pulling the value of “index” from playerprefs and using it blindly to access cambiarPersonaje by index, without first verifying that the index exists. Add Debug.Log statements to output the value of index and the length of cambiarPersonaje.

If that is not the line, then tell us what line is 25. Also, use CODE tags when posting code on the forums, so people don’t need you to tell us line numbers.

yes, it is line 25, i don’t understand you, Could you send me how the code would look?

Add this right before that line:

Debug.Log("index: " + index.ToString() + " cambiarPersonaje.Length: " + cambiarPersonaje.Length.ToString());

The value of index needs to be equal to or greater than 0, and less than cambiarPersonaje.Length, or you will get the error you are seeing.

Thank you. In one more minute I’ll let you know how it went

1 Like

No sé dónde tengo que poner, ¿en la línea 24?


justo antes de la línea con el error.

índice: 1 cambiarPersonaje.Longitud: 0, it is the result

What do I do with the result?

So index has a value of 1, while the array is empty. There is no index 1 in that array, so you get an error.

It could be that the second line of Start has transform.childCount returning 0. So no children, when you obviously expect there to be children.

Why there would be no children when you expect them? Don’t know. Look at the object this script is attached to. That would become a problem in the scene rather than a code issue. You might even have this script attached to the wrong object, or multiple objects.

i don’t understand you

So in your code you have this line:

cambiarPersonaje = new GameObject[transform.childCount];

It creates a new array of GameObjects with the length of transform.childCount. What is the value of transform.childCount? You can use Debug.Log to check. I suspect the value is 0.

If that value is 0, you need to figure out why, but that won’t be in code, it will be in the scene or prefab, or wherever you are attaching this script.

Just a note, in the future when you run into confusing code issues, a good first step is to just start adding Debug.Log statements everywhere to output the value of all the variables you are using in that area of the code. You do that, and 9 times out of 10 the cause of the problem becomes immediately obvious. You’ll see what order your code is being called, what values are being used by the code, etc, etc. Usually you’ll then notice something in that output which is different than you expected it to be, and then quickly figure out why with that information. Then you only spend your time banging your head on that 1 out of 10 problem which is far more difficult :slight_smile:

Good luck :smile:

Thanks you