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

Fill List Error: Index Out Of Range

Discussion in 'Scripting' started by Pedro_R, Apr 10, 2019.

  1. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    Hello,

    I have a Player Class which inherits from ScriptableObject and has a List<Vector3> in it. Then I have another script running and I'm trying to fill that List<Vector3> in Start(), but I keep getting an error which says that the index is out of range. Here are my codes:

    class Player : Scriptable Object
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : ScriptableObject
    6. {
    7.     //Some variables.
    8.     public int playerID;
    9.     public List<Vector3> ownedSlots = new List<Vector3>();
    10.     public string symbol;
    11.  
    12.     public void Setup(int id, string sym)
    13.     {
    14.         playerID = id;
    15.         symbol = sym;
    16.     }
    17. }
    class Mechanics : MonoBehaviour:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.TextCore;
    5.  
    6. public class Mechanics : MonoBehaviour
    7. {
    8.     //Some variables.
    9.     [SerializeField] [Range(2, 4)] int playerCount = 2;
    10.     [SerializeField] string[] playerSymbols = new string[4];
    11.     List<Player> players = new List<Player>();
    12.  
    13.     Awake()
    14.     {
    15.     //Some code.
    16.     }
    17.     void Start()
    18.     {
    19.         //Some code.
    20.         for (int i = 0; i < playerCount; i++)
    21.         {
    22.             if (playerSymbols[i] == "")
    23.                 playerSymbols[i] = i.ToString(); //I tested and everything is working fine to this line.
    24.             players[i] = ScriptableObject.CreateInstance("Player") as Player;  //This is where things go wrong.
    25.             players[i].Setup(i, playerSymbols[i]);
    26.         }
    27.     }
    28.     //Some code.
    29. }
    30.  
    The error I get is:

    ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
    System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
    System.Collections.Generic.List`1[T].set_Item (System.Int32 index, T value) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
    Mechanics.Start () (at Assets/Mechanics.cs:29)

    Can anyone help me?

    Thanks!
     
    Last edited: Apr 10, 2019
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    368
    List players is empty, you can't assign players[0] because it doesn't exist. Try players.add(ScriptableObject...)
     
    eliteforcevn likes this.
  3. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    Thanks, it worked!

    I did players.add(ScriptableObject.CreateInstance("Player") as Player);
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I'd recommend instead using the generic version of CreateInstance rather than the string one you're using.

    Code (csharp):
    1. players.Add(ScriptableObject.CreateInstance<Player>());
    The reason I say this is that .AddComponent() has already deprecated the string-taking variant:

    https://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

    and it wouldn't surprise me if CreateInstance(string) will also go away soon.
     
  5. warnerjonn

    warnerjonn

    Joined:
    Jun 29, 2020
    Posts:
    2
    The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method. Indexing an empty list will always throw an exception. Use a method like Add to append the item to the end of the list, or Insert to place the item in the middle of the list somewhere, etc. You cannot index into a list if that offset doesn't exist.

    Typically, an ArgumentOutOfRangeException results from developer error. Instead of handling the exception in a try/catch block, you should eliminate the cause of the exception or, if the argument is returned by a method call or input by the user before being passed to the method that throws the exception, you should validate arguments before passing them to the method.