foreach Player error

Options
Hi guys, I have a problem with a foreach bucle I made, this is the code:
  foreach (Photon.Realtime.Player Player in PhotonNetwork.PlayerList)
            {
                int GameCount = (int)PhotonNetwork.LocalPlayer.CustomProperties["GameCount"];
                UnorderedList.Add(GameCount);
                print("Player");
            }
So, in theory, as the documentation says, this PhotonNetwork.PlayerList property has inside, all the players In the room.
So, by following this logic, this foreach should repeat 2 times if there are 2 players, if there are only 4 players, the loop should repeat 4 times, right?

But this is not happening, it repeats A LOT of times, as you can see I have a print line, this message printed 70 times in the console, meaning that the loop repeated 70 times until I stopped the debug.

This is giving me wrong values, does someone know why is this happening and how can I fix it?

Comments

  • Hi @SebGM2018,

    So, by following this logic, this foreach should repeat 2 times if there are 2 players, if there are only 4 players, the loop should repeat 4 times, right?


    Yes, exactly.

    But this is not happening, it repeats A LOT of times, as you can see I have a print line, this message printed 70 times in the console, meaning that the loop repeated 70 times until I stopped the debug.


    When and from where do you call this loop?
  • SebGM2018
    Options

    Hi @SebGM2018,

    So, by following this logic, this foreach should repeat 2 times if there are 2 players, if there are only 4 players, the loop should repeat 4 times, right?


    Yes, exactly.

    But this is not happening, it repeats A LOT of times, as you can see I have a print line, this message printed 70 times in the console, meaning that the loop repeated 70 times until I stopped the debug.


    When and from where do you call this loop?
    Thanks for your reply, Im calling this from a method that is only called once in the start function.
  • Is that script attached to an object which gets (network) instantiated at runtime? Is that script more than once in the scene?