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.
!(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)
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
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].
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.