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

What's wrong with my Index?

Discussion in 'Scripting' started by Lancaster13, Feb 3, 2015.

  1. Lancaster13

    Lancaster13

    Joined:
    Dec 24, 2014
    Posts:
    25
    Hello guys,

    This is my script, and ever when i start, this return a "ArgumentOutOfRangeException: Argument is out of range" Whats wrong with my declaration?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Reflection;
    5. using System.IO;
    6. using System.Text;
    7. using System.Collections.ObjectModel;
    8.  
    9. namespace Tudo
    10. {
    11.     public class Luccas : MonoBehaviour
    12.     {
    13.         void Start()
    14.         {  
    15.             Outra Mesmo = new Outra ();
    16.             int HP = Mesmo [0] = 10;
    17.  
    18.             Debug.Log (HP);
    19.         }
    20.     }
    21.  
    22.     public class Outra
    23.     {
    24.         public int Hp;
    25.         public string Nick;
    26.         public int status;
    27.  
    28.         public List<int> numeros = new List<int> ();
    29.  
    30.         public int this [int Index]
    31.         {
    32.             get
    33.             {
    34.                 return numeros[Index];
    35.             }
    36.             set
    37.             {
    38.                 numeros[Index] = value;
    39.             }
    40.         }
    41.     }
    42. }
     
  2. meatpudding

    meatpudding

    Joined:
    Jan 28, 2015
    Posts:
    39
    Code (csharp):
    1. new List<int> ();
    You're not adding any elements to numeros, so it can't get the first index.
     
  3. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    meatpuddding is right..so maybe you can add an Outra constructor and
    add some element to the list first.
     
  5. Lancaster13

    Lancaster13

    Joined:
    Dec 24, 2014
    Posts:
    25
    Yeah, he is right. I add few elements in the List, and work right. Thanks!