Search Unity

Help Wanted password field not automatically updating when i want to show/hide

Discussion in 'Scripting' started by lz7cjc, Oct 15, 2021.

  1. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    398
    Hi
    Sorry for the basic question but I can't get my password field to toggle between showing text and asterisks when using a toggle option on the screen. This is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class showPassword : MonoBehaviour
    6. {
    7.  
    8.     public InputField passwordField;
    9.     public Toggle show;
    10.     public void update()
    11.     {
    12.         Debug.Log("am i showing? " + show.isOn);
    13.         if (show.isOn)
    14.         {
    15.             Debug.Log("switching to showing");
    16.             passwordField.contentType = InputField.ContentType.Standard;
    17.         }
    18.  
    19.         else
    20.         {
    21.             Debug.Log("switching to hiding");
    22.             passwordField.contentType = InputField.ContentType.Password;
    23.         }
    24.     }
    25. }
    I did also try calling separate functions rather than putting in update() but neither seem to work
    (when i click in the password field after choosing show/hide, it switches)

    Any ideas?
    thanks
     
    Last edited: Oct 15, 2021
  2. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    175
    it's Update with uppercase U
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,056
    Note OP: getting this right should be handled for you by the intellisense in Visual Studio, if properly configured.

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
    Nefisto likes this.
  4. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    398
    thanks - i made that change and will take a look at your suggestions
    but unfortunately it didn't fix the issue
    this is how i set it up in the editor 2021-10-15 (2).png
     
  5. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    175
    Okay, we have misunderstood some things here, I'll try to give you some tips too, so let's begin:
    1- [Optional] Don't be a hipster, use standards, class and methods should be capitalized.
    2- Update() method is a special kind of method, this is a message that unity gives to us to allow us to run our logic in a frame by frame step, the list of other "magic" methods is here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html. So don't name your personal methods with those names.
    3- Your code is working, if you change your toggle value and click on the input field again you'll see that it is working as expected, what is happening is that the field isn't being "refreshed", we can solve this calling ForceLabelUpdate() in our input field
    4- [Optional] You're getting a reference to your toggle and at the same time registering in OnValueChange event, this isn't necessary, are you seeing that On Value Changed has a boolean in front of it, this means that if you pass to it a method that also has a boolean as a parameter you will be able to add this in a dynamic way, which means that every time that you change your toggle value this method will receive the current toggle value, so if we have something like

    public void MyMethodThatIsntNamedUpdate(bool toggleValue) {}
    we'll see this
    upload_2021-10-15_12-5-15.png
    now we don't need the reference to the toggle ;P
    5- [Optional] It's better to go with TMP (Text Mesh Pro) versions of UI things, unless you have a good reason to don't


    So this is it, now the full code, make sure to register it in the dynamic section in On Value Changed toggle event:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.     public InputField passwordField;
    7.  
    8.     public void MyMethodThatIsntNamedUpdate(bool toggleValue)
    9.     {
    10.         // If this is strange do it in the normal if/else way
    11.         passwordField.contentType = toggleValue ?
    12.             InputField.ContentType.Standard :
    13.             InputField.ContentType.Password;
    14.    
    15.         passwordField.ForceLabelUpdate();
    16.     }
    17. }
    --N
     

    Attached Files:

    lz7cjc likes this.
  6. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    398
    fantastic - thanks so much for your help and also the resources... really useful stuff
     
unityunity