-1

I have a dictionary with gameobjects as the keys, which is n amount and gameobjects as the values, which is amount of possible 4.

    Dictionary<GameObject, ArrayList> polesAttachedToFloor = new Dictionary<GameObject, ArrayList>();

public void AddFloor(GameObject floor)
{
    if(floor != null)
    {
        polesAttachedToFloor.Add(floor, new ArrayList { null,null,null,null });
    }
}


public void AddPole(GameObject floor, GameObject pole)
{
    for (int i = 0; i <= 4; i++)
    {

    }

}

How do I iterate through the "values?"... and is there a more 'appropriate' way of doing what Im aiming for?

user1932940
  • 41
  • 1
  • 1
  • 5
  • Possible duplicate of [What is the best way to iterate over a Dictionary in C#?](http://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary-in-c) – Koby Douek Mar 10 '17 at 06:24
  • 1
    You definitely should not use `ArrayList` - use either regular array `new GameObject[4]` (if you need all 4 references, presumably one per corner) or `List` (if you just need list of variable length)... – Alexei Levenkov Mar 10 '17 at 06:29
  • How would I iterate through the array or arraylist in the dictionary in this case though? – user1932940 Mar 10 '17 at 06:34

3 Answers3

0
ArrayList poles;
if (polesAttachedToFloor.ContainsKey(floor))
{
   poles = dictionary[floor];
}

if(poles!=null)
{
   for (int i = 0; i <= 4; i++)
   {
      poles.Add(pole);
   }
}

Try that inside you AddPole function.

maximelian1986
  • 2,308
  • 1
  • 18
  • 32
  • But if you provide more info about what is your idea of using that one I can generate better code. I think you should encapsulate your floor into separate class and that class should have list/array of the poles, and then you should have FloorManager class which have list/array/dictionary of Floor class instances. Also you can add some Action<> method inside FloorManager and subscribe some of Floor methods to it so if you want to call something on all of the Floor. – maximelian1986 Mar 10 '17 at 06:38
  • Also you can save only class instances to array and from them you can access gameObject with gameObject property (I think it called property). – maximelian1986 Mar 10 '17 at 06:39
  • What im doing is assigning a possible of 4 gameobjects to each nth gameobject. If i have a floor it can have four poles and i need to be able to find the poles according to which floor it is. There can be any amount of floors but only 4 poles to each floor. – user1932940 Mar 10 '17 at 06:41
0

There is how you should encapsulate that...Dont have unity running so may be some typos.

public class FloorManager:MonoBehaviour
    {
        public gameObject floorPrefab;

        private List<Floor> floors;

        void Start()
        {
            floors = new List<Floor>();
        }

        public void AddFloor()
        {
            //instantiate prefab // gameObject floorGo = Instantiate....
            Floor floorScript = floorGo.getComponent<Floor>();
            floors.Add(floorScript);
            floorScript.AddPoles();

        }

        public void RemoveFloor(Floor floor)
        {
            floors[floors.IndexOf(floor)].gameObject.Destory();
            floors.Remove(floor);
        }
    }

    public class Floor : MonoBehaviour
    {

        public gameObject polePrefab;

        public Pole [] poles = new Pole[4];

        public void AddPoles()
        {
            for (int i = 0; i < 4; i++)
            {
                //instantiate prefab // gameObject poleGo = Instantiate....

                Pole poleScript = poleGo.getComponent<Pole>();
                poles[i]=poleScript;
            }
        }

    }

    public class Pole : MonoBehaviour
    {
        //some logic here if needed...Destroy...Damage...
    }
maximelian1986
  • 2,308
  • 1
  • 18
  • 32
0

Play with Enumerator :D Have fun

public void AddPole(GameObject floor, GameObject pole)
    {
        var enumerator = polesAttachedToFloor.GetEnumerator ();
        for (int i = 0; i <= 4; i++)
        {
            if (enumerator.MoveNext ()) {
                var item = enumerator.Current;
            }
        }

    }
Cổ Chí Tâm
  • 330
  • 1
  • 7