Getting Player color from Game Manager into PlayerHealth script

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?

the fillImage is an image script? Try get fillImage **.**GetComponent().color=

yes , i’m just tweaking the code from the tanks tutorial. In line 18 if the colors are hard coded it works fine. But I want it to use the colors chosen in the Game Controller.

Try this. fillImage **.**GetComponent().color= Color.Lerp (playerColor, playerColor, currentHealth / startingHealth);

Also I noticed that in the first image the “Player Colors” are transparent (black line under the color).
Edit: Change “private void SetHealthUI()” to a
IEnumerator and yield until the Lerp is done.

No luck with those.

The actual mechanism works when it is hard coded:

fillImage.color = Color.Lerp (Color.green, Color.green, currentHealth / startingHealth);

It’s just that I want to access the colors in the m_Players.PlayerColor variable instead

I cannot understand how your Lerp is working…Lerp is going from one color to another in steady rate.
When your player colors are generated?
I said before that your Player Colors are transparent. So if you assign the you cannot see them. Did you checked that?

1 Like

Sorry I missed this - this has half fixed my problem :smile:

As you can see though it has coloured them both according to Player 2 (Element 1).

This is interesting because I’m having trouble getting them to fire missiles separately (related question link) . When one fires, the other does also. I wonder if these problems are linked to the Game Manager or Player Manager? And not separating out each player properly?

From a quick look at this thread, you loop through the array and assigned the colour to the player on each iteration. That’s not what you want… you want to make sure only 1 gets assigned to player 1 and the other to player 2.

Thanks.

So do you think this is because my array elements are not lining up, i.e 0:1, 1:2.

I’m confused as to why my player 2 colour is assigned to both pkayers, and it has skipped the first element slot, but yet they are spawning in the correct places. I. E. Player 1 is spawning from element 0, and player 2 from 1.

Well, this is what your code does… let’s imagine.

Gets list of colours, which is Green , Blue.

Now, for player 1 and 2, both do this:
assign personal colour = green, assign personal colour = blue.

In other words, at the end of the loop, they both have ‘Blue’ from this little example I posted :slight_smile:

Aaaaaah! Thanks! Makes sense.