Search Unity

Dictionary of gameObjects with Integers as keys

Discussion in 'Scripting' started by 4wall, Nov 23, 2017.

  1. 4wall

    4wall

    Joined:
    Sep 16, 2014
    Posts:
    73
    I have the following public fields referencing GameObjects in my script

    Code (csharp):
    1.  
    2.  public GameObject tile0;
    3.     public GameObject tile1;
    4.     public GameObject tile2;
    5.     public GameObject tile3;
    6.     public GameObject tile4;
    7.     public GameObject tile5;
    8.     public GameObject tile6;
    9.     public GameObject tile7;
    10.     public GameObject tile8;
    11.  
    I need to be able to reference these gameObjects by integer values.
    So an int value of 0 would point to tile0, etc.

    Can anyone help with setting up the dictionary properly?
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Just need a list for this mate, since you can use the index to reference the gameobjects.
    Code (CSharp):
    1.  
    2. //This is required to use lists
    3. using System.Collections.Generic;
    4.  
    5. //This is how you initialize the list
    6. public List <GameObject> tiles = new List<GameObject> ();
    7.  
    8. //Let's say you want to do something with tile 4. You can access the list like this.
    9. tiles [4].SetActive (false);