Search Unity

Change placeholder color if InputField is not empty ( != null )

Discussion in 'Scripting' started by FreshCoder-731319, Aug 3, 2020.

  1. FreshCoder-731319

    FreshCoder-731319

    Joined:
    May 21, 2020
    Posts:
    17
    Where am i going wrong with changing my Input-field placeholder color if if a user has entered a text.

    Default placeholder color is grey
    User inputs text
    Placeholder white

    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class InputField : MonoBehaviour
    4. {
    5.     public InputField inputField;
    6.  
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         if (inputField != null)
    12.         {
    13.             inputField.GetComponent<Image>().material.color = Color.blue;
    14.  
    15.         }
    16.     }
    17. }
    18.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    First, I wouldn't do this in Update.

    InputFields include an OnEndEdit event or an OnValueChanged event that would work much better for this. Either check the text property of the InputField when edit is done and see if it has any characters (recommended) or when the value changes in case you want to change it as they type or delete characters.

    By default, InputFields don't have a material. So if you just plan to change the color of the Image, you should remove the .material part.

    And of course, make sure your inputField variable actually has something dragged and dropped into it.

    However, you mention Placeholder in your description, but you are targeting the Image. If you want to target the Placeholder text, you need to target it instead.

    Code (CSharp):
    1. inputField.placeholder.color = Color.blue;
    This should change the color of the placeholder text instead if that is what you want to do.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Are you asking about the color being not blue, or are you asking about the fact that your logic for checking if the field is empty is not right?

    What I see off the bat is that your logic for checking if it is not empty is not right. Try this:

    Code (CSharp):
    1. if (inputField.textComponent.text.Length > 0) {
    2.   // change the color
    3. }
     
    FreshCoder-731319 likes this.
  4. FreshCoder-731319

    FreshCoder-731319

    Joined:
    May 21, 2020
    Posts:
    17
    Thanks for the reply! Probably did not make it clear. Im not changing the text color, i am trying to change the actually inputfield background colour to go from one colour to another. I will update the forum with a screenshot
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Create an Image variable in your script and drag the appropriate image component from the hierarchy into that field. Then change the color of that component in the script.
     
  6. FreshCoder-731319

    FreshCoder-731319

    Joined:
    May 21, 2020
    Posts:
    17
    Thanks I had to make more edits related to naming conventions but thanks your response it helped! Just a question..How would i go about retreving the original colour after changing the inputfield.

    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class InputField1 : MonoBehaviour
    4. {
    5.     public InputField mainInputField;
    6.  
    7.  
    8.  
    9.     void Start()
    10.     {
    11.         defaultColour = GetComponent<Image>().color = Color.white;
    12.     }
    13.  
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.         if (mainInputField.textComponent.text.Length > 0)
    20.         {
    21.             mainInputField.GetComponent<Image>().color = Color.white;
    22.  
    23.         }
    24.         else
    25.         {
    26.  
    27.         }
    28.     }
    29. }
    30.  
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    What you have is close. Capture it in a variable in Start(). Then just set it back when the field is empty:

    Code (CSharp):
    1. using UnityEngine.UI;
    2. public class InputField1 : MonoBehaviour
    3. {
    4.     public InputField mainInputField;
    5.     Color defaultColour;
    6.     void Start()
    7.     {
    8.         defaultColour = GetComponent<Image>().color;
    9.     }
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         var imageComponent = mainInputField.GetComponent<Image>();
    14.         if (mainInputField.textComponent.text.Length > 0)
    15.         {
    16.             imageComponent.color = Color.white;
    17.         }
    18.         else
    19.         {
    20.             imageComponent.color = defaultColour;
    21.         }
    22.     }
    23. }
    What @Brathnann said about using events instead of Update() is valid, but I also wouldn't worry about it for now if this works for you, especially if you're a beginner. It's an optimization for later. Good luck!