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.
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.
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