-1

In my scoreboard UI I have a box that is just an image with Score_Entries inside as children. There are 4 children, one for each player in the game. When the scoreboard starts up I have a function that assigns the text inside the Score_Entries Ui elements. To do this I have an array called Text[] playerNames and my UI elements are dragged into this in the inspector.

int i = 0;
foreach (Player test in PhotonNetwork.PlayerList)
{
    playerNames[i].text = test.NickName;
    allscores[i].text = 0.ToString();
    i++;
    Debug.Log("Text inside the array: " + playerNames[i].text);
    Debug.Log("Player Name: " + test.NickName);
}

As you can see I have set the text inside my first Text element to equal a name of a player. However, what should happen is in the Debug statements the Player Name line output and the Text inside the array output should Show me the string stored inside test.NickName. say test is players name. playersNames[i].text should == test currently it shows playerNames[i] = TEST NAME which is my default text when creating the UI. However, in the game, the name of the player is shown correctly as test not TEST NAME.

I dont see how this is working can someone help?

PWND
  • 409
  • 3
  • 11
  • 1
    A bit confused about your `i++` in the middle between assigning the array and logging ... you will always try and log the **next** item of your arrays ... not those you just assigned – derHugo Mar 30 '21 at 10:04
  • I coded the logic first then added the Debug statements to see where it was going wrong. I missed I was using the i in the statements – Amrik Ahluwalia Mar 30 '21 at 10:16

1 Answers1

1

the problem is i++ put i++ after

Debug.Log("Text inside the array: " + playerNames[i].text);
Debug.Log("Player Name: " + test.NickName);
i++
bx1
  • 36
  • 3
  • Thanks! My next issue is I am using this array in other code but It cannot see the new name that I have changed – Amrik Ahluwalia Mar 30 '21 at 10:25
  • you have to change to foreach (Player test in PhotonNetwork.PlayerList) { playerNames[i].text = test[i].NickName; allscores[i].text = 0.ToString(); i++; Debug.Log("Text inside the array: " + playerNames[i].text); Debug.Log("Player Name: " + test[i].NickName); } – bx1 Mar 30 '21 at 10:37
  • I want to access the array in another script so I am expecting playerNames[0] to be equal to test.NickName however it is TEST NAME. So would I access it by doing playerNames[i].NickName? – Amrik Ahluwalia Mar 30 '21 at 10:56
  • Why does it need to be test[i] compared to just test.text? Does the for each loop not get each text as an object? – Amrik Ahluwalia Mar 30 '21 at 11:12