Strange probably silly error with arrays.

Hello this is my first time posting an error in a forum and english is not my first language so i apologize beforehand if i say (or in this case, type) something wrong.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ListaDeFrases : MonoBehaviour
{
    // Start is called before the first frame update
  public static ListaDeFrases ldf;
    public  string[] frases = new string[7];
    void Awake()
    {
        ldf =  this;
    }
    void Start()
    {
   
    frases[0] = "Bienvenido a mi juego";
    frases[1] = "puede que no te guste";
    frases[2] ="y puede que también quieras dejar de jugar.";
    frases[3] = "pero recuerda, que yo no te obligo.";
    frases[4] = "Habiendo dicho esto";
    frases[5] = "empecemos";
    frases[6] = "Este persona tiene caracteristicas digamos, umm";
    }

    // Update is called once per frame
    void Update()
    {
       
    }
   
}

!(http:///home/dude/Imágenes/Captura de pantalla de 2022-03-02 19-26-39.png)
I don’t know if the picture is loaded but is an screenshot of the error in unity’s console, it says “IndexOutOfRangeException: Index was outside the bounds of the array” when i double click on it, it sends me to the line 31. But my array “frases” has a lenght of 7 (if i make it bigger the error persists)

if i comment the line 31 the script work, but i need to get the array bigger (in the future it’s probably going to be like 300 phrases)

Thanks beforehand.

Forgot to say i’m using Unity 2020.3.28f1
and Visual Studio code version 1.64.2.

The array is public and serializable. Is it also that size in the inspector?
If not then that is your problem. Serialized values override script defined values

1 Like

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps to success:

  • find which collection it is (critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection
  • remember you might have more than one instance of this script in your scene/prefab

Regardless, what @MaskedMouse says is likely an issue:

Field initializers versus using Reset() function and Unity serialization:

Serialized properties in Unity are initialized as a cascade of possible values:

  • what the class constructor makes (field initializers)

  • what is saved with the prefab

  • what is saved with the prefab override(s)/variant(s)

  • what is saved in the scene and not applied to the prefab

  • what is changed in Awake(), Start(), or even later etc.

So you want to make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

You wrote [ ]icode]public string[ ] frases = new string[7]; [/icode] but your array is public so it’s serialized to the inspector. That means that your new string[7]; will be thrown out in favor of what’s in the inspector, and that’s an array of size 0 by default.

You don’t want the array to be exposed to the inspector if you intend to initialize it yourself. I would make it private instead of public. If you need it to be public for some reason, use
[HideInInspector].

I didn’t realize about the array showing in the inspector, as a programmer i’m probably going to starve to death.

Thanks didn’t know about that, i was soo (inside?) into the code that i really didn’t pay attention to the inspector. Changed the length of the array from there and now its work.