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. Dismiss Notice

UnityC# - Odd ArrayIndexOutOfRange error when transferring variables of my object instances

Discussion in 'Scripting' started by raykooooo, Mar 22, 2016.

  1. raykooooo

    raykooooo

    Joined:
    Feb 7, 2016
    Posts:
    24
    Hello, now I know what ArrayOutOfIndexRange means but I know don't know why the issues have occurred here.

    So I have A few scripts set-up for my project.
    My "EnemyMovement" Script is attached to each instances of my enemy icons on-scene, it allows movement of my enemies depending on their location....I have set-up some arrays to store the variables necessary to activate the "EnemyMovement" script...

    I'll show the main issues of the situation as of now: (If the other script is wanted, I will be editing it in later.)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class GameManage : MonoBehaviour {
    6.         public static PlayerMovement PTurnInstance;
    7.         public static GameObject[] ETurnInstance;
    8.         public static EnemyMovement E;
    9.         [HideInInspector] public bool PlayerTurn;
    10.         [HideInInspector] public bool EnemyMoving;
    11.         public int[] EnemyAP;
    12. //int array for storing int values of each enemies from another script
    13.         public Vector3[] Enemypos;
    14. //Vector3array for storing position values of each enemies from another script
    15.         public RaycastHit2D[] Ehit;
    16. //RaycastHit2D array for storing raycasthitsD values of each enemies from another script
    17.         public  List<EnemyMovement> Enemies;
    18.         public int EAPSum;
    19.  
    20. void Start(){
    21.         E = GameObject.FindGameObjectWithTag ("Enemy").GetComponent <EnemyMovement> ();
    22.         PTurnInstance = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMovement>();
    23.         PlayerTurn = PTurnInstance.PlayerTurnStart;
    24.         ETurnInstance = GameObject.FindGameObjectsWithTag("Enemy");
    25.  
    26. //instanciations for each of my enemy object on-scene...
    27.  
    28.         for(int i=0;i<ETurnInstance.Length;i++){
    29.             AddEnemyToList (ETurnInstance[i].GetComponent<EnemyMovement>());
    30.         }
    31.  
    32. void FixedUpdate () {
    33.    
    34.  
    35.         if (PlayerTurn) {
    36.             EnemyMoving = false;
    37.             print ("From GameManage{MovingEnemy()}, PlayerTurnEnd?=" + PTurnInstance.PlayerTurnStart);
    38.             return;
    39.         }
    40.         else {        
    41.             EnemyMoving = true;
    42.             StartCoroutine(MovingEnemies());
    43.        }
    44.     }
    45.  
    46.  
    47. public void AddEnemyToList(EnemyMovement Script) {
    48.         Enemies.Add(Script);
    49.         print("Enemies added " + Enemies.Count);
    50.     }
    51. IEnumerator MovingEnemies(){
    52.    
    53.         if (Enemies.Count == 0) {
    54.             EnemyMoving = false;
    55.             print ("No enemies to move");
    56.             yield return new WaitForSeconds (1f);
    57.         }
    58.         for (int i = 0; i< Enemies.Count; i++) {
    59.             print ("Enemy list: "+Enemies.Count);
    60.             print ("EnemyMovement listed: "+ETurnInstance.Length);
    61.  
    62. //Prints my Array indexes for debugging, result was as expected, equal to the enemies objects on-scene - 2 in my case
    63.  
    64.             EnemyAP [i] = ETurnInstance[i].GetComponent <EnemyMovement>().AP_E;
    65.  
    66. //Issue lies here, Array index is out of range yet my ETurnInstance array or Enemies list are at the expected size...2
    67.             Enemypos [i] = ETurnInstance[i].GetComponent <EnemyMovement>().pos_E;
    68.             Ehit [i] = ETurnInstance[i].GetComponent <EnemyMovement>().hitE;
    69.             print ("Moving Enemies: "+ Enemies[i]);
    70.             Enemies[i].DetermineDirection(EnemyAP[i],Enemypos[i],Ehit[i]);
    71.             EAPSum += EnemyAP [i];
    72.             yield return new WaitForSeconds (1F);
    73.         }
    74.  
    75.         if(EAPSum<=0){
    76.             E.EnemyTurnOver ();
    77.         }
    78.         yield return new WaitForSeconds (1F);
    79.       }
    80.  
    81. }
    82.  

    - So I am using "i" a lot in the for-loop : "EnemyAP "[ i ]" = ETurnInstance"[ i ]".GetComponent <EnemyMovement>().AP_E;" So what is the error in this expression?

    - I am trying to store the public int value "AP_E" in a component of each Enemy Object into the array....

    Thank you guys in advance!
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Your naming is confusing. There are naming conventions, they're surely not a must but it's easier for people to help after all...

    Just make sure all the arrays have the size that they need.
    Your comment says "ETurnInstance array or Enemies list are at the expected size", what about Enemypos?
    Try to initialize all the arrays with length 'ETurnInstance.length' after line 24.

    In fact, you'd probably be better off if you create a class that holds the information for each entity. You'd only have one array instead of 4-5.
     
  3. raykooooo

    raykooooo

    Joined:
    Feb 7, 2016
    Posts:
    24
    Hm I'll try this and update for any problems...


    yeah..I tend to name things in a weird way...(not too experienced in the practice)...

    Thank you ~