Hey there,
in my project I’m creating several player characters for a turn-based couch game (think offline version of Super Mario Party). The most important stats of each player are displayed at the right side of the screen. That includes the name, current points, and its color. To do so, after creating the players, I generate the UI via code. For each player I generate an Info Panel and set its corresponding values.
The name and the initial points are displayed correctly, but the color always shows white. I’ve logged the code and the code line receives the correct color values, but the Image.color either doesn’t take the values or resets them to white at the end of the frame. Can someone explain to me why this happens and how I can solve this problem?
I’ll add some code elements below but because the “start off” signal is somewhere else entirely and a chain reaction of events eventually leads to this moment, I’ll skip some parts that seem irrelevant to me. I cann add them though, if necessary.
// PlayerManager
private void CreatePlayers()
{
for (int i = 0; i < players.Length; i++)
{
GameObject playerGO = Instantiate(playerPrefab, position, rotation, parent);
playerGO.GetComponent<PlayerInfo>().SetInfos(id, playerNames[i], color);
playerGO.SetActive(false);
playerCharactersByName.Add(playerNames[i], playerGO);
}
OnPlayersCreated?.Invoke(new List<GameObject>(playerCharactersByName.Values));
}
// InfoDisplayManager is called by the OnPlayersCreated event
private void InitWalletDisplays(List<GameObject> playerCharacters)
{
foreach (GameObject character in playerCharacters)
{
GameObject infoDisplay = Instantiate(infoDisplayPrefab, infoGrid);
infoDisplay.GetComponent<InfoDisplay>().Setup(character);
}
}
// InfoDisplay
public void Setup(GameObject playerCharacter)
{
nameText.text = playerCharacter.name; // this displays the correct name
// grabbing the color from the material...
Color c = playerCharacter.GetComponentInChildren<MeshRenderer>().material.color;
// ... or the Info class leads to the same results
Color c = playerCharacter.GetComponent<PlayerInfo>().PlayerColor;
Debug.Log(c); // this prints the correct color RGBA values
colorImage.color = c; // this image stays white
}
The result looks like this. Capsule color and Player Info color are correct. The small bar on the left-hand side of the info panel is still white.