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

int return gives ArgumentOutOfRangeException

Discussion in 'Scripting' started by HVutov, May 17, 2015.

  1. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    Hello when i return the value it gives me this
    ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
    Parameter name: index
    Code (CSharp):
    1.  
    2. public class Enemy : MonoBehaviour {
    3.     List<int> dmgs = new List<int>();
    4.     GenerateItem itemId;  
    5.             if (hp <= 0)
    6.             {
    7.                 int id = itemId.generateItemByMonsterLevel();
    8.                 control.inventoryArray.Add(id);
    9.                 Debug.Log(hp);    
    10.             }
    11.  
    12.  
    13. using UnityEngine;
    14.  
    15. class GenerateItem : MonoBehaviour
    16. {
    17.    
    18.   public int generateItemByMonsterLevel()
    19.   {
    20.   return Random.Range(0, 7);
    21.   }
    22. }
    23.  
    24.  
     
  2. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    I assume that the error occurs when running control.inventoryArray.Add(id)

    You are leaving out crucial information such as the whole control class and therefore we can't really see where the problem is, since it appears that you aren't using your dmgs List, rather you're using "inventoryArray" in control.
     
    Kiwasi likes this.
  3. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    no the problem comes at line 7 it dont even go to line 8
    ive remove the code lines that are not important only line 7 is important now
     
  4. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Enemy : MonoBehaviour {
    7.     GenerateItem itemId;
    8.     public GameObject Player;
    9.     public GameObject WeaponObj;
    10.  
    11.     public Animator swordAnim;
    12.     public GUIText myGuiText;
    13.  
    14.     private Weapon weapon;
    15.  
    16.     private float radius = 1;
    17.     private float tempDiff = 0;
    18.     private float damageTaken;
    19.     private decimal swordSwingTime = 0.30m;
    20.  
    21.     private bool isHitted = false;
    22.     private bool isGrounded = false;
    23.     private bool isLeft = false;
    24.     private bool isDetected = false;
    25.  
    26.     private int hp = 100;
    27.  
    28.     private GameControl control;
    29.  
    30.  
    31.  
    32.  
    33.     void OnCollisionEnter2D(Collision2D coll)
    34.     {
    35.         gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
    36.         isGrounded = true;
    37.    
    38.     }
    39.     void OnTriggerStay2D(Collider2D coll)
    40.     {
    41.      
    42.         if (coll.gameObject.tag == "Weapon" && swordAnim.GetBool("attack") == true && isHitted == false)
    43.         {
    44.          
    45.             isHitted = true;
    46.             weapon = WeaponObj.GetComponent<Weapon>();
    47.             damageTaken = Random.Range(weapon.strenght / 2, weapon.strenght / 2 + weapon.strenght);
    48.             int damageTakenToInt;
    49.             int critNum = Random.Range(1, 100);
    50.             if (critNum >= (weapon.critical / 2) - 30 && critNum <= (weapon.critical / 2) + 30)
    51.             {
    52.                 damageTaken *= 2;
    53.                 damageTakenToInt = (int)damageTaken;
    54.                 FloatingText.Show(string.Format("{0}", damageTakenToInt), "DamageTextCrit", new FromWorldPointTextPositioner(Camera.main, transform.position, 1.5f, 50));
    55.             }
    56.             else
    57.             {
    58.                 damageTakenToInt = (int)damageTaken;
    59.                 FloatingText.Show(string.Format("{0}", damageTakenToInt), "DamageText", new FromWorldPointTextPositioner(Camera.main, transform.position, 1.5f, 50));
    60.             }
    61.  
    62.             hp -= damageTakenToInt;
    63.             if (hp <= 0)
    64.             {
    65.                 int id = itemId.generateItemByMonsterLevel();
    66.                 control.inventoryArray.Add(id);
    67.              
    68.                 Debug.Log(hp);
    69.              
    70.             }
    71.  
    72.          
    73.  
    74.         }
    75.     }
    76.  
    77.     // Use this for initialization
    78.     void Start () {
    79.         control = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameControl>();
    80.     }
    81.  
    82.     // Update is called once per frame
    83.     void Update () {
    84.         detectPlayer();
    85.       //  jump();
    86.      
    87.         if (isHitted == true)
    88.         {
    89.             if (swordSwingTime != 0)
    90.             {
    91.                 swordSwingTime -= 0.01m;
    92.             }
    93.             else
    94.             {
    95.                 swordSwingTime = 0.30m;
    96.                 isHitted = false;
    97.             }
    98.         }
    99.     }
    100.  
    101.     public void jump()
    102.     {
    103.         if (isGrounded == true)
    104.         {
    105.             isGrounded = false;
    106.             gameObject.GetComponent<Rigidbody2D>().isKinematic = false;
    107.             transform.Translate(Vector3.up * 50 * Time.smoothDeltaTime);
    108.         }
    109.      
    110.     }
    111.  
    112.     public void detectPlayer()
    113.     {
    114.         float diff = transform.position.x - Player.transform.position.x;
    115.         if (diff < 0)
    116.         {
    117.             isLeft = true;
    118.             string diffInString = diff.ToString();
    119.             string newDiff = diffInString.Substring(1);
    120.             diff = float.Parse(newDiff);
    121.         }
    122.  
    123.      
    124.  
    125.         if (diff < 1.5 && diff>0.25 && isDetected == false)
    126.         {
    127.             Vector3 playerPos = Player.transform.position;
    128.             Vector3 newPlayerPos = new Vector3(playerPos.x,transform.position.y,transform.position.z);
    129.             transform.position = Vector3.MoveTowards(transform.position, newPlayerPos, 1 * Time.deltaTime);
    130.          
    131.        
    132.         }
    133.     }
    134.  
    135.    
    136.  
    137.  
    138. }
    139.  
    140.  
     
  5. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    Perhaps its having an issue that you are returning Random.Range(0,7) which is a float value. Try casting it to an int before you return it in generateItemByMonsterLevel
     
  6. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    No change ive never had problem with that the method returns int i even cast it its the same
     
  7. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    Well, this is strange. I have never seen an ArgumentOutOfRangeException that wasn't with an array or list, but you say that it occurs on the line where you call itemId.generateItemByMonsterLevel(), right?

    One error that I see with your code is that you declare GenerateItem itemId but you never initialize itemId to a specific instance of GenerateItem. But, you should get a null reference exception in that case, not an ArgumentOutOfRangeException.
     
    HVutov and Kiwasi like this.
  8. HVutov

    HVutov

    Joined:
    Mar 16, 2015
    Posts:
    18
    Yes i forgot to initialize it thank you so much