I have a Game Manager (GameController.cs) attached to an empty GameObject in my scene, a Player Manager, and a Player Health script attached to my player game objects.
My ‘Player Colors’ are set in the scene view as below:
I believe these are stored in a PlayerManager array accessed by my GameController script
GameController.cs
public class GameController : MonoBehaviour {
public GameObject m_PlayerPrefab;
public PlayerManager[] m_Players;
private PlayerManager m_RoundWinner;
private PlayerManager m_GameWinner;
private void SpawnAllPlayers()
{
for (int i = 0; i < m_Players.Length; i++)
{
m_Players[i].m_Instance = Instantiate (m_PlayerPrefab, m_Players [i].m_SpawnPoint.position, m_Players [i].m_SpawnPoint.rotation) as GameObject;
m_Players[i].m_PlayerNumber = i + 1;
m_Players[i].Setup ();
}
}
PlayerManager.cs
[Serializable]
public class PlayerManager {
public Color m_PlayerColor;
public Transform m_SpawnPoint;
[HideInInspector] public int m_PlayerNumber;
[HideInInspector] public string m_ColoredPlayerText;
public void Setup()
{
m_CanvasGameObject = m_Instance.GetComponentInChildren<Canvas> ().gameObject;
m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB (m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>";
MeshRenderer[] renderers = m_Instance.GetComponentsInChildren<MeshRenderer> ();
for (int i = 0; i < renderers.Length; i++) {
renderers [i].material.color = m_PlayerColor;
}
}
I am trying to access the m_PlayerColor variable from my PlayerHealth script (attached to the instantiated prefab) to color the players health bar using the set ‘Player Color’
PlayerHealth.cs
public class PlayerHealth : MonoBehaviour {
private PlayerManager[] m_Players;
private Color playerColor;
void Awake () {
m_Players = GameObject.Find ("GameController").GetComponent<GameController> ().m_Players;
for (int i = 0; i < m_Players.Length; i++)
{
playerColor = m_Players [i].m_PlayerColor;
}
}
private void SetHealthUI(){
fillImage.color = Color.Lerp (playerColor, playerColor, currentHealth / startingHealth);
}
}
But it’s not doing anything.
I’m not getting any errors, but the health car is just the grey sprite image rather than the stored colored values.
Any ideas?