Search Unity

How to change player's color more than once

Discussion in 'Scripting' started by RetroboltGames, Jun 4, 2019.

  1. RetroboltGames

    RetroboltGames

    Joined:
    Oct 26, 2018
    Posts:
    11
    When changing the player's color, I can only do it once.

    Code (CSharp):
    1. private SpriteRenderer rend;
    2.  
    3.     public Color colorToTurnTo = Color.white;
    4.  
    5. void Update()
    6.     {
    7.         if (Input.GetKeyDown(KeyCode.L))
    8.         {
    9.             rend = GetComponent<SpriteRenderer>();
    10.             rend.color = colorToTurnTo;
    11.  
    12.         }
    13.  
    14.  
    15.     }
     
  2. TheRealRan

    TheRealRan

    Joined:
    Jun 3, 2019
    Posts:
    18
    Should try to explain a bit more what you want to do.

    Do you just want to be able to change the colorToTurnTo variable?

    I would create a method called ChangeColor (or ChangePlayerColor if the class you are in isn't a Player class) that contains your above code.

    Code (CSharp):
    1. private void ChangeColor(Color toColor)
    2. {
    3.     rend = GetComponent<SpriteRenderer>();
    4.     rend.color = toColor;
    5. }
    This will allow you to call ChangeColor whenever you need it without copying the above code everywhere. Also, if you intend to use the method a lot, it would probably be better for you to do the GetComponent inside the Awake method. That way you will always have a reference to the SpriteRenderer without needing to call GetComponent everytime. Then again, that depends on your code and what you are doing with it. I will assume it is attached right away on the player and you won't be changing it.

    Code (CSharp):
    1. private void Awake()
    2. {
    3.     rend = GetComponent<SpriteRenderer>();
    4. }
    Thus, the ChangeColor method will change:

    Code (CSharp):
    1. private void ChangeColor(Color toColor)
    2. {
    3.     rend.color = toColor;
    4. }
    Now you will want to do a SetColorToTurnTo method, that you will call whenever you want a new color. I am not sure if this is what you want, but as not much info was given I'll assume it is.

    Code (CSharp):
    1. private void SetColorToTurnTo(Color color)
    2. {
    3.     colorToTurnTo = color;
    4. }
    Whenever you want to you will now be able to call SetColorToTurnTo and once you have your desired color, pressing L (Update) will call ChangeColor and change the player color to your desired color.

    The code should be as follows:

    Code (CSharp):
    1.  
    2. {
    3.     private SpriteRenderer rend;
    4.     public Color colorToTurnTo = Color.white;
    5.  
    6.     private void Awake()
    7.     {
    8.         rend = GetComponent<SpriteRenderer>();
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         if(Input.GetKeyDown(KeyCode.L))
    14.         {
    15.             ChangeColor(colorToTurnTo);
    16.         }
    17.     }
    18.  
    19.     // You can also remove the toColor parameter and do rend.color = colorToTurnTo; if you wish to
    20.     private void ChangeColor(Color toColor)
    21.     {
    22.         rend.color = toColor;
    23.     }
    24.  
    25.     // Called whenever you want another color than white, make it public if needed
    26.     private void SetColorToTurnTo(Color color)
    27.     {
    28.         colorToTurnTo = color;
    29.     }
    30. }
    If this is not what you want, would you be able to explain it with a little bit more detail? Right now there isn't much for us to know what you want exactly.
     
    Last edited: Jun 4, 2019
  3. RetroboltGames

    RetroboltGames

    Joined:
    Oct 26, 2018
    Posts:
    11