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 I'm getting IndexOutOfRangeException error

Discussion in 'Scripting' started by unity_FB1DF262867B0CF500AF, Jun 11, 2022.

  1. unity_FB1DF262867B0CF500AF

    unity_FB1DF262867B0CF500AF

    Joined:
    Mar 4, 2022
    Posts:
    2
    I'm getting an error called IndexOutOfRangeException. I'm not exactly sure why. Here are my scripts. Also, the error is on line 33 in the collisionDetection.cs script according to the debugger in Unity. The array that is giving me issues only has three values. So the index should be 0, 1, and 2. Correct?
     

    Attached Files:

  2. unity_FB1DF262867B0CF500AF

    unity_FB1DF262867B0CF500AF

    Joined:
    Mar 4, 2022
    Posts:
    2
    Heres the code in collisionDetection.cs for those who don't wanna download didn't know it would do that. Sorry.


    Code (CSharp):
    1. using Unity;
    2. using UnityEngine;
    3. using System;
    4. using UnityEditor;
    5. using System.Text;
    6.  
    7. public class collisionDetection : MonoBehaviour {
    8.    
    9.     public VarHolder GM;
    10.  
    11.     /* string CollisionDetected(Collider Collision) {
    12.         if(Collision.tag == "l1walls") {
    13.             return "Wall detected";
    14.         }
    15.         else if (Collision.tag == "l1victpad") {
    16.             return "Victory Pad Detected";
    17.         }
    18.         else {
    19.             return "No detections";
    20.         }
    21.     } */
    22.  
    23.     // runs method on start up of application when attached to object
    24.     private void Start() {
    25.  
    26.         GM = GameObject.Find("GameManager").GetComponent<VarHolder>();
    27.     }
    28.  
    29.     private void OnTriggerEnter(Collider other) {
    30.         Debug.Log(other.name);
    31.         string CollisionDetectedAnswer = other.tag;
    32.         Debug.Log($"Checking");
    33.         if (CollisionDetectedAnswer == GM.CollisionCheck[0]) {
    34.             Debug.Log($"Check One: \n \t Wall Collision Detected");
    35.             GM.CA.wallCollision();
    36.         }
    37.         else if(CollisionDetectedAnswer == GM.CollisionCheck[1]) {
    38.             Debug.Log($"Check Two: \n \t Victory pad collision detected");
    39.             GM.CA.victPadCollision();
    40.         }
    41.         else if (CollisionDetectedAnswer == GM.CollisionCheck[2]) {
    42.             // Do nothing //
    43.             Debug.Log($"Check Three: \n \t No collision detected");
    44.         }
    45.         else {
    46.             Debug.Log(GM.CollisionCheck);
    47.         }
    48.     }
    49. }
    Heres the code in collisionAction.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class collisionAction : MonoBehaviour {
    7.     VarHolder GM;
    8.  
    9.     private void Start() {
    10.         GM = GameObject.Find("GameManager").GetComponent<VarHolder>();
    11.         for(int i = 0; i <= GM.pos.Length; i++) {
    12.             LevelStartPosition[i] = 0;
    13.         }
    14.     }
    15.  
    16.     Vector3 LevelStartPosition;
    17.  
    18.    
    19.  
    20.     public void RetrieveLevelPosition() {
    21.         Debug.Log($"Retrieving Position Data");
    22.         for(int i = 0; i <= GM.pos.Length; i++) {
    23.             LevelStartPosition[i] = GM.pos[i];
    24.         }
    25.         Debug.Log($"Retrieved Position Data");
    26.     }
    27.  
    28.     public void wallCollision() {
    29.         if (LevelStartPosition[0] != 0 && LevelStartPosition[1] != 0 && LevelStartPosition[2] != 0 && !GM.MenuCheck) {
    30.             Debug.Log($"Level Start Positions Obtained.");
    31.             GM.MovePlayer.ToLevelStartPosition(LevelStartPosition);
    32.         }
    33.         else {
    34.             RetrieveLevelPosition();
    35.             wallCollision();
    36.         }
    37.     }
    38.     public void victPadCollision() {
    39.  
    40.     }
    41. }
    42.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,969
    CORRECT! Now make sure you're talking about the same instance of that array.

    Remember, you might have this script in more than one place, intentionally or inadvertently.

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

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

    Steps to success:
    - find which collection it is (critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection
    - remember you might have more than one instance of this script in your scene/prefab