Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Iterating over Dictionary of (Int, Class)

Discussion in 'Getting Started' started by BWaySimon, Apr 15, 2024.

  1. BWaySimon

    BWaySimon

    Joined:
    Oct 22, 2023
    Posts:
    5
    Trying to iterate over a Dictionary of (int, Class) Classes - and cannot for the life of me figure out HOW to reference a method in the script of the current iterated Object "CheckDirection()", which returns String. I have another Dictionary with the coords, but I don't iterate over that.
    Everytime I step through the foreach loop, I get the same Object (the first one with index 0), never the others?
    Am I trying to do something that is not allowed? Or am I doing it wrong?
    Short sample of code.

    Happy to supply any details required, this is not a secret game or anything. Just me fooling about, trying to learn :)

    Code (CSharp):
    1.  
    2. //  moveString is either Up, Down, Left or Right  //
    3. string newdirection;
    4. foreach (KeyValuePair<int, Block> block in GridManager.Instance._blocks)
    5. {
    6.     //  Open the CheckDirection() Method on current Object  //
    7.     newdirection = block.Value.CheckDirection(moveString);
    8.     int val = block.Key;
    9.     string[] blockstr = block.Value.ToString().Split(" ");
    10.     _startposition = new Vector2(GridManager.Instance._blocksPos[val].x, GridManager.Instance._blocksPos[val].y);
    11.    _vector2 = GetNextMove(newdirection, blockstr[0], _startposition.x, _startposition.y);
    12.    _finishposition = _vector2;
    13. }