Can somebody help me with a UI login system.

hello im making a project for school. and it will be an mobile app… im working for 3 days to make a UI login system but the problem is i dont know how to link the UI textbox area to the script for checking if the username and the password is correct… the code is here :

var Username = "";
var Password = "";

var OwnerUser = "user";
var OwnerPass = "pass";

var userCORRECT = false;
var passCORRECT = false;

var userArea = gameObject;
var passArea = gameObject;

function CheckUser()
{
    if(userCORRECT == OwnerUser)
    {
        userCORRECT = true;
    } else {
    
        userCORRECT = false;
    }
}

function CheckPass()
{
    if(passCORRECT == OwnerPass)
    {
        passCORRECT = true;
    } else {
        passCORRECT = false;
    
    }
}

function Update()
{

    // here needs function for if you press on the button and if the user and pass is correct it will load a new scene.
    if(userCORRECT == true)
    {
        if(passCORRECT == true)
        {
            Application.LoadLevel("A_Level");
        }
    }
}

function AreaUI()
{
    // here needs a function for if you type in the UI inputField that if you type the correct username and pass it will automaticly set it to true. if its not false.

}

if you can help me i will say thx for helping me…

Make a public function (in UnityScript that’s a normal function) with the parameter for a string.
function OnSubmit(username) {// Like so.
//What to do with the value from input field
}
Then in the editor, go to the Username field that I assume you’ve created and there should be a End Edit list.
All you do is simply point the script to the object the script contains and it will let you select the OnSubmit function.

You can also point it to the On Value Change list, which will continuely call that function until the user is done editing the field.

You can also do it this way: (This is C#)

  • private InputField usernameInputField =null;

  • private Button loginButton = null;

    • private void Start() {
  • // Add listener to catch the submit (when set login button is pressed)

  • usernameInputField.onSubmit.AddListener((username) => OnSubmit(username));

  • // Add validation

  • usernameInputField.validation = InputField.Validation.Alphanumeric;

  • // This is a setup for a button that grabs the field value when pressed

  • loginButton.onClick.AddListener(() => OnSubmit(usernameInputField.value));

  • }

  • public void OnSubmit(string username) {

  • //What to do with the value from input field

  • }

Do not forget to add using UnityEngine.UI;

okay i will try your method i hope it will work…

the problem is still that if i type in the Input field it wont go to true…

and here is the over view :

edit :

username field isnt working…
and also the password field isnt working…

2282072--153224--Overview.PNG

this is better and my script is like this now :

var Username = "";
var Password = "";

var OwnerUser = "user";
var OwnerPass = "pass";

var userCORRECT = false;
var passCORRECT = false;

var userArea = gameObject;
var passArea = gameObject;

public function OnSubmit(Username)
{
   
}




function CheckUser1()
{
    if(OwnerUser == Username)
    {
        if(Username == userCORRECT)
        {
            if(userCORRECT == true)
            {
                userCORRECT = true;
            } else {
                userCORRECT = false;
            }
        }
    }
}

function CheckPass()
{
    if(passCORRECT == OwnerPass)
    {
        passCORRECT = true;
    } else {
        passCORRECT = false;
       
    }
}

function Update()
{
   
    // here needs function for if you press on the if the user and pass is correct and loads the level.
    if(userCORRECT == true)
    {
        if(passCORRECT == true)
        {
            Application.LoadLevel("A_Level");
        }
    }
}

function AreaUI()
{
    // here needs a function for if you type in the UI inputField that if you type the correct username and pass it will automaticly set it to true. if its not false.
   
}

2282077--153228--Overview_functions.PNG

C# example.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
class LoginPanel : MonoBehaviour {
   public InputField usernameField = null; // Drag & drop the username field here.
   public InputField passwordField = null; // Drag & drop the password field here.
   public Button loginButton = null; // Drag & drop the login button here.

   private string username = "";
   private string password = "";

   private string ownerUsername = "user";
   private string ownerPassword = "pass";

   private bool userCORRECT = false;
   private bool passCORRECT = false;

   private void OnEnable() {
     if(usernameField == null) {
       Debug.Log("Drag & drop the username field into the inspector.");
     }
     else {
       usernameField.onEndEdit.AddListener((username) => OnSubmit(username, "username"));
     }

     if(passwordField == null) {
       Debug.Log("Drag & drop the username field into the inspector.");
     }
     else {
       passwordField.onEndEdit.AddListener((username) => OnSubmit(username, "password"));
     }

     if(loginButton == null) {
       Debug.Log("Drag & drop the username field into the inspector.");
     }
     else {
       loginButton.onClick.AddListener(() => OnSubmit(null, "login"));
     }
   }

   private void Update() {
     if(userCORRECT == true && passCORRECT == true) {
       Application.LoadLevel("A_Level");
     }
   }

   public void OnSubmit(string value, string type) {
     if(type == "username") {
       username = value;

       if(value == ownerUsername) {
         userCORRECT = true;
       }
     }
     else if(type == "password") {
       password = value;

       if(value == ownerPassword) {
         passCORRECT = true;
       }
     }
     else if(type == "login") {
       if(username == ownerUsername) {
         userCORRECT = true;
       }
       if(password == ownerPassword) {
         passCORRECT = true;
       }
     }
   }
}

I changed the code, forgot to add identifiers to OnSubmit function.

1 Like

thx for posting this but i have 3 errors…

error 1 : LoginPanel.cs(52,30) error CS1041: identifier expected
error 2 : LoginPanel.cs(52,37) error CS1041: Identifier exprected
error 3 : LoginPanel.cs(52,30) error CS0100: The Parameter name ‘NeedSomeGeneratorHere’ is a duplicate

I updated the code.

thx it works no errors