• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by NIMBLE_JIM · Oct 07, 2015 at 08:55 PM · inputfieldpasswordfield

How to set up a password to be used in the password input field?

Hi,

I would like to set up a password login for my game. I would simply like the user to put their password in, then the level load when it is correct.

I previously used the following script -

 #pragma strict
 
 var password : String = "";
 function OnGUI () {
 
 password = GUI.PasswordField (Rect (Screen.width/2, Screen.height / 1.95, 256, 32), password, "*" [0], 25);
 
 if (password == "password123") 
 {
 
 Application.LoadLevel("Level_1");
 
 }
 
 }

T$$anonymous$$s works fine, but I would like to be able to use it with the 4.6+ UI.

I know to add an InputField, then choose the type to 'Password'. However I am slightly lost for what to do next, I assume a script has to be set up to attach to the 'On Value Change (String)', but not sure what it is.

I have searched for a tutorial that deals with t$$anonymous$$s specifically, but cannot find one that gives a step by step guide for setting it up. I'm new to coding, so would like to know exactly what to do. If someone could help me, or at least point me in the direction of such a tutorial, it would be much appreciated.

Many thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FortisVenaliter · Oct 07, 2015 at 09:10 PM

Oh, the new system is great, you'll see. Just write a script to handle the change. Give it a function that takes a single parameter (a string).

Then, add that script to a gameobject. In the InputField settings, under OnValueChanged, click the plus button. Then drag the object with your new script to the field. Then, in the drop-down on the right, select your script and function.

Now, when the text changes, it will call that function, and you can respond to it however you like.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NIMBLE_JIM · Oct 08, 2015 at 10:04 AM 0
Share

Hi FortisVenaliter,

Thank you very much for your prompt response, I now have a clearer understanding of how it works. However I am a little stuck on writing the script, I have tried the following one from the scripting API but this just adds a new GUI text box in and automatically loads the level if I add in "Application.LoadLevel("Level_1");"

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public string passwordToEdit = "My Password";
     void OnGUI() {
         passwordToEdit = GUILayout.PasswordField(passwordToEdit, "*"[0], 25);
     }
 }

Would you be able to assist me with the writing of the script?

Many thanks

avatar image OctoMan NIMBLE_JIM · Oct 08, 2015 at 10:51 AM 0
Share

I recommend use the Unity 4.6+ new Canvas system as @FortisVenaliter mentioned. Create the input field in the canvas and do what he said.

Create a function which takes that input and reacts to it.

example c# :

     public void PassWordCheck(string incomingPassword)
     {
     if (incomingPassword == "password123")
     {
     Application.LoadLevel("Level_1");
     }
 }

Now connect the Input field as he said with OnValueChanged, and select the function from the gameobject where this script is on, and that should work.

Cheers

avatar image NIMBLE_JIM · Oct 08, 2015 at 11:03 AM 0
Share

That works perfectly @OctoMan! Thanks very much for your help :D

avatar image OctoMan NIMBLE_JIM · Oct 08, 2015 at 11:09 AM 0
Share

You're welcome.

avatar image NIMBLE_JIM OctoMan · Oct 12, 2015 at 09:52 AM 0
Share

Hi @OctoMan, I have one other quick query, I have now edited the script so it reads from the localisation plugin I have -

 public class PasswordInput2 : MonoBehaviour {
 
         public void PassWordCheck(string incomingPassword)
     {
         var RealPassword = I2.Loc.ScriptLocalization.Get("MapsPassword", false);
 
 
         if (incomingPassword == RealPassword)
         {
             Application.LoadLevel("Level_1");
 
     }
 }
 }

This works perfectly, however I would like to make it display a message if the password is incorrect, so I would like to attach a gameobject to the script and activate it if the user presses 'RETURN' and the password is wrong. I have moved the script from 'On Value Change' to 'End Edit' so that it waits for something to be pressed, however I am a little stuck on getting it to display the gameobject (error message). I have adjusted the script to the following...


 public class PasswordInput2 : MonoBehaviour {
 
     public void GameObject(GameObject);
 
     public void PassWordCheck(string incomingPassword)
     {
         var RealPassword = I2.Loc.ScriptLocalization.Get("MapsPassword", false);
 
 
         if (incomingPassword == RealPassword)
         {
             Application.LoadLevel("Level_1");
 
             else 
             {
                 gameObject.SetActive(true);
             }
     }
 }
 }

This returns the following errors (attachment)...

alt text

Any ideas?

Many thanks in advance.

error-log.png (12.8 kB)
Show more comments
avatar image NIMBLE_JIM · Oct 12, 2015 at 05:52 PM 0
Share

@OctoMan

Thanks, I also needed to change line 6 to public GameObject myObject; and line 13 to myObject.SetActive(true);

So it now reads...

 public class PasswordInput2 : MonoBehaviour {
 
         public GameObject myObject;
 
     public void PassWordCheck(string incomingPassword)
     {
         var RealPassword = I2.Loc.ScriptLocalization.Get("MapsPassword", false);
 
 
         if (incomingPassword == RealPassword)
         {
             Application.LoadLevel("Level_1");
 myObject.SetActive(false);
         }
         else
         {
             myObject.SetActive(true);
 
     }
 }
 }

Thanks again.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unlock game with provided password / serial key 1 Answer

Access gameobject from a different scene... 1 Answer

How do i convert inputfield input to float ? 0 Answers

Is there a way to set the mass of a gameobject without using the Rigidbody component?? 2 Answers

The UI Button Hitbox changes after using a Input Field on Android 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges