I have a password box set up in my project with the content type set to 'Password', so it shows asterisks when the user types in the box. However I would like a toggle switch so the user can click to show the password characters if they like, so effectively toggling between 'Password' and 'Standard' - is this possible? Any pointers would be appreciated.

Unity 5.3.5 - PC Standalone.

Thanks in advance.


Yes, it's possible. I assume you have a simple scene set up with a Toggle and an InputField like this:

enter image description here

Add a new Empty to your scene and name it UIController. Select your newly created object, head over to the Inspector and hit the Add Component button to create and attach a new custom component. Name the script UIController and paste the following code (I'm assuming C#):

using System;
using UnityEngine;
using UnityEngine.UI;

public class UIController : MonoBehaviour {

    public UnityEngine.UI.InputField passwordInput = null;

    public void ToggleInputType() {
        if (this.passwordInput != null) {
            if (this.passwordInput.contentType == InputField.ContentType.Password) {
                this.passwordInput.contentType = InputField.ContentType.Standard;
            } else {
                this.passwordInput.contentType = InputField.ContentType.Password;

            this.passwordInput.ForceLabelUpdate ();

The script's public method is basically the answer to your question. Probably one thing to mention is the call to forceLabelUpdate after switching the InputField's contentType, otherwise the display-mode doesn't update immediately.

Just to be complete:

  1. Of course you have to wire the reference to your specific InputField. To do this, select your UIController in the outline to have the UIController's properties visible in the Inspector. Then drag&drop the InputField to link it to the 'Password Field' member:

enter image description here

  1. And finally, build the bridge from the Toggle to the UIController's ToggleInputType() method. Select the Toggle and scroll all the way down to the bottom:

enter image description here

From this section (outlined in blue) you can specify any object in your scene to be informed when the user interacts with the Toggle. Here you want to choose the UIController as the object (green) and its ToggleInputType method (red) to be invoked.

Wow. This got longer than expected but I hope it helps! :)

  • \$\begingroup\$ Thanks very much for taking the time to write that up, it's much appreciated. I shall give it a go and let you know how I get on. \$\endgroup\$
    Nov 15 '16 at 23:32
  • \$\begingroup\$ i was missing ForceLabelUpdate(). thank you! \$\endgroup\$ Feb 22 '19 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.