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

Question "Index was outside the bounds of the array" when using an array inside a singleton

Discussion in 'Scripting' started by zlombardo001, Dec 19, 2022.

  1. zlombardo001

    zlombardo001

    Joined:
    Dec 14, 2022
    Posts:
    3
    Hello everyone.
    I'm using a singleton for my Main Manager, and I currently have two arrays like this.
    upload_2022-12-18_22-49-25.png

    I have a function to update currentHearts (inside this same MainManager file), and when I try to change the value of any item in the array, I get the "Index was outside the bounds of the array" error, even if I use 0 as the index, which should work (I think) considering that I'm declaring the currentHearts array using new int[4], and initializing it with all the values as 0.
    upload_2022-12-18_22-51-19.png

    I suspect that there's probably something that I'm missing in relation to how variables work when using this type of class, but I've been trying to solve this since yesterday and I have looked through a ton of forum posts and haven't been able to understand what's wrong.

    Am I putting the values in the array the wrong way? Though I feel that if that was the case, then the error would be that the value is null, not that the index is out of bounds. I don't know.

    Thank you.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    You initialise these arrays with four elements. Ergo, indexes 0, 1, 2, and 3.

    In your second screenshot of code I can operating through the bounds of less than or equal to four. See the problem?

    Also for future, please don't use screenshots of code and post copy-paste of code using code-tags.
     
  3. zlombardo001

    zlombardo001

    Joined:
    Dec 14, 2022
    Posts:
    3
    Sorry, will do next time.
    qNum is not related to the bounds. It's another unrelated number. qNum is just a variable I'm using to store a score for a minigame. The amount that is added to currentHearts is related to that number, in this case I'm just increasing it by one. I'm using a different variable for the index, and since it was giving me this error, I tried to hardcode it by using 0, and either way, it's telling me the index is out of bounds.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,955
    Also you're not even using qNum to index currentHearts in that snippet... you're just using 0.

    Therefore, IF that is actually the line then it is a zero-sized array.

    That makes me think your arrays are wiped out by scene / prefab serialization.

    Serialized properties in Unity are initialized as a cascade of possible values, each subsequent value (if present) overwriting the previous value:

    - what the class constructor makes (either default(T) or else 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.

    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.

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

    https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908

    https://forum.unity.com/threads/crouch-speed-is-faster-than-movement-speed.1132054/#post-7274596

    OTHERWISE...

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

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

    Steps to success:
    - find which collection it is and what line of code accesses it <--- critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection size
    - remember you might have more than one instance of this script in your scene/prefab
    - remember the collection may be used in more than one location in the code
     
  5. zlombardo001

    zlombardo001

    Joined:
    Dec 14, 2022
    Posts:
    3
    Ah thanks so so much! The issue was indeed with serialization. It completely escaped my mind. I checked in Unity and it was initializing the array as an empty array instead of the int[4] I was trying to create.
    I can't believe I didn't think of this earlier.

    I used the 0 as index just to debug, since I was sure that the index 0 had to exist if everything went well. qNum is just a variable I use to store a score.

    Thanks again.
     
    Kurt-Dekker likes this.