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");
}
}
This 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 this 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
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.
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 : $$anonymous$$onoBehaviour {
public string passwordToEdit = "$$anonymous$$y Password";
void OnGUI() {
passwordToEdit = GUILayout.PasswordField(passwordToEdit, "*"[0], 25);
}
}
Would you be able to assist me with the writing of the script?
$$anonymous$$any thanks
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 inco$$anonymous$$gPassword)
{
if (inco$$anonymous$$gPassword == "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
That works perfectly @Octo$$anonymous$$an! Thanks very much for your help :D
Hi @Octo$$anonymous$$an, I have one other quick query, I have now edited the script so it reads from the localisation plugin I have -
public class PasswordInput2 : $$anonymous$$onoBehaviour {
public void PassWordCheck(string inco$$anonymous$$gPassword)
{
var RealPassword = I2.Loc.ScriptLocalization.Get("$$anonymous$$apsPassword", false);
if (inco$$anonymous$$gPassword == 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 : $$anonymous$$onoBehaviour {
public void GameObject(GameObject);
public void PassWordCheck(string inco$$anonymous$$gPassword)
{
var RealPassword = I2.Loc.ScriptLocalization.Get("$$anonymous$$apsPassword", false);
if (inco$$anonymous$$gPassword == RealPassword)
{
Application.LoadLevel("Level_1");
else
{
gameObject.SetActive(true);
}
}
}
}
This returns the following errors (attachment)...
Any ideas?
$$anonymous$$any thanks in advance.
@Octo$$anonymous$$an
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 : $$anonymous$$onoBehaviour {
public GameObject myObject;
public void PassWordCheck(string inco$$anonymous$$gPassword)
{
var RealPassword = I2.Loc.ScriptLocalization.Get("$$anonymous$$apsPassword", false);
if (inco$$anonymous$$gPassword == RealPassword)
{
Application.LoadLevel("Level_1");
myObject.SetActive(false);
}
else
{
myObject.SetActive(true);
}
}
}
Thanks again.
Your answer
![](https://koobas.hobune.stream/wayback/20220612040429im_/https://answers.unity.com/themes/thub/images/avi.jpg)