Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I have a problem

Discussion in 'Scripting' started by Jaime516, Apr 8, 2021.

  1. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    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");
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Which line is line 25? Is it this line?
    Code (csharp):
    1. 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.
     
  3. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    yes, it is line 25, i don't understand you, Could you send me how the code would look?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add this right before that line:
    Code (csharp):
    1. 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.
     
  5. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    Thank you. In one more minute I'll let you know how it went
     
    Joe-Censored likes this.
  6. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    No sé dónde tengo que poner, ¿en la línea 24?
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847

    justo antes de la línea con el error.
     
  8. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    índice: 1 cambiarPersonaje.Longitud: 0, it is the result
     
  9. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    What do I do with the result?
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  11. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    i don't understand you
     
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So in your code you have this line:
    Code (csharp):
    1. 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.
     
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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 :)

    Good luck :D
     
  14. Jaime516

    Jaime516

    Joined:
    Apr 8, 2021
    Posts:
    8
    Thanks you