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

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:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class showPassword : MonoBehaviour
{

    public InputField passwordField;
    public Toggle show;
    public void update()
    {
        Debug.Log("am i showing? " + show.isOn);
        if (show.isOn)
        {
            Debug.Log("switching to showing");
            passwordField.contentType = InputField.ContentType.Standard;
        }

        else
        {
            Debug.Log("switching to hiding");
            passwordField.contentType = InputField.ContentType.Password;
        }
    }
}

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

it’s Update with uppercase U

1 Like

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://discussions.unity.com/t/778503

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://discussions.unity.com/t/805330/7

1 Like

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

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: Unity - Scripting API: MonoBehaviour. 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) {}

![7575109--938407--upload_2021-10-15_12-5-15.png|494x449](upload://ckBxtDiHaZ3zVkaxpU3zRDvgQn.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:

```csharp
using UnityEngine;
using UnityEngine.UI;
 
public class Example : MonoBehaviour
{
    public InputField passwordField;

    public void MyMethodThatIsntNamedUpdate(bool toggleValue)
    {
        // If this is strange do it in the normal if/else way
        passwordField.contentType = toggleValue ?
            InputField.ContentType.Standard :
            InputField.ContentType.Password;
    
        passwordField.ForceLabelUpdate();
    }
}

–N

7575109--938410--upload_2021-10-15_12-7-18.png

2 Likes

fantastic - thanks so much for your help and also the resources… really useful stuff