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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Trying to reference an array but getting a out of bounds

Discussion in 'Scripting' started by tawdry, May 28, 2018.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Hi
    Me again trying now to get the value of an array.
    This code sets up the size of array and assigns it values

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Doorlock : MonoBehaviour {
    6.     public int Doordiff;
    7.     GameObject g;
    8.     thieving stole;
    9.     public int tumblers;
    10.     public int [] code;
    11.     public int answer;
    12.     //public thieving Thieving;//GETS REFERENCE TO SCRIPT ON OTHER GAMEOBJECT
    13.         void Start () {
    14.        
    15.         Doordiff = 6;
    16.         //Debug.Log (Thieving.steal);
    17.         GameObject g = GameObject.Find("thief");
    18.         stole=g.GetComponent<thieving>();
    19.     //    Debug.Log (stole.steal);
    20.         tumblers = Doordiff - stole.steal;
    21.     //    Debug.Log (tumblers);
    22.  
    23.         if (tumblers < 3) {
    24.             tumblers = 3;
    25.                 }
    26.         int [] code = new int [tumblers];
    27.         //Debug.Log (tumblers);
    28.         for(int i = 0; i <tumblers;i++)
    29.         {answer=Random.Range (1, 5);
    30.            
    31.             code [i] = answer;
    32.             //    Debug.Log (code[i]);
    33.                
    34.  
    35.         }
    36.     }
    37.    
    38.     // Update is called once per frame
    39.     void Update () {
    40.        
    41.     }
    42. }
    Then this code is trying to read the first value of the array but getting array out of range
    IndexOutOfRangeException: Array index is out of range.
    up.OnTriggerEnter () up.cs:18)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class up : MonoBehaviour {
    6.     int solution=1;
    7.     GameObject tumb;
    8.     Doorlock pick;
    9.     // Use this for initialization
    10.     void Start () {
    11.         tumb = GameObject.Find("Knob");
    12.         pick=tumb.GetComponent<Doorlock>();
    13.     }
    14.    
    15.  
    16.     void OnTriggerEnter() {
    17.    
    18.         Debug.Log (pick.code[1]);
    19.         if (pick.code[0]==1){Debug.Log ("woot");}
    20.         if (pick.code[1]!=1){Debug.Log ("nope");}
    21.     }
    22. }
    23.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This line of code is not what you want:
    Code (csharp):
    1. int [] code = new int [tumblers];
    It overrides your class level variable. Just write this:
    Code (csharp):
    1. code = new int [tumblers];
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    When looping an array don't you need to loop over the array.Length - 1?

    Or is this lists?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's never the case. So long as the condition is "< length" (well, and you want to loop over them all, obviously*).
     
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Brilliant thx would not have spotted that.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome.