Search Unity

ArgumentOutOfRangeException: Argument is out of range.

Discussion in 'Scripting' started by EliteWalrus, Aug 2, 2015.

  1. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    The error seemingly occurs at random times, usually during rounds 3 or 4.



    Code (CSharp):
    1.     IEnumerator StartNextRound () {
    2.         coroutineRunning = true;
    3.         yield return new WaitForSeconds (5);
    4.         round += 1;
    5.         StartCoroutine (RoundNotification ());
    6.         roundTotalText.text = round.ToString ();
    7.         if (round <= 10) {
    8.             zombiesToSpawn += 2.4f;
    9.             zombieHealth += 100;
    10.         }
    11.         if (round > 10) {
    12.             zombiesToSpawn = 24 * round * 0.15f;
    13.             zombieHealth *= 1.1f;
    14.         }
    15.         zombiesToSpawn = (int)Mathf.RoundToInt (zombiesToSpawn);
    16.  
    17.         for (int i = 0; i < zombiesToSpawn; i ++) {
    18.             int randomSpawner = Random.Range (0, activeSpawners.Count);
    19.             Vector3 spawnLocation = new Vector3 (activeSpawners[randomSpawner].transform.position.x + Random.Range (0f, 4f),
    20.                                                  activeSpawners[randomSpawner].transform.position.y,
    21.                                                  activeSpawners[randomSpawner].transform.position.z + Random.Range (0f, 4f));
    22.             GameObject spawnedZombie = (GameObject) Instantiate (Resources.Load ("Zombie"), spawnLocation, activeSpawners [randomSpawner].transform.rotation);
    23.             zombies.Add (spawnedZombie);
    24.             zombies[i].GetComponent <Zombie> ().health = zombieHealth;
    25.             yield return new WaitForSeconds (Random.Range (0, 5));
    26.         }
    27.         coroutineRunning = false;
    28.     }
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    EDIT - After looking at the docs, random range wont include the max, so the below is wrong.

    I think this is the error
    Code (CSharp):
    1.  int randomSpawner = Random.Range (0, activeSpawners.Count);
    change it to this
    Code (CSharp):
    1.  int randomSpawner = Random.Range (0, activeSpawners.Count - 1);
    Would explain why it seems to be happening at random as well.
     
    EliteWalrus likes this.
  3. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Oh, I thought Random.Range was (inclusive, exclusive), but you're right, thanks man.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Code (csharp):
    1. zombies[i].GetComponent<Zombie>().health= zombieHealth;
    This is a pointless complication- just use GetComponent on the spawnedZombie reference instead.

    What HiddenMonk says is probably correct- I think the documentation for this is wrong.
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I edited my post, I think I was wrong.
    Edit - unless if its true that the docs are wrong
    Edit 2 - after testing, I think the docs are right, so the error must be from something else, or has your issue stopped?
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Do a check to make sure the that the activeSpawners list isn't zero.

    Btw, the documentation WAS wrong, but is NOW correct, which was what caused the confusion there.
     
  7. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Ok, the problem did persist until I did what Lysander recommended and now everything works as intended.

    Just to be clear on Random.Range; this is the line I should use?
    Code (CSharp):
    1. int randomSpawner = Random.Range (0, activeSpawners.Count);
    Or should I subtract 1 from activeSpawners.Count?
     
  8. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    do not subtract 1, the max will not be included. The docs are correct.
     
    BenZed, DonLoquacious and EliteWalrus like this.