Hello!
I’m new to unity and i’m trying to to create a simple register system.I created a database with usernames and passwords and i want to pass input values from unity to database.
My input fields(UI → Input Field) are three: username, password and confirm password.
But when i call my function CreateUser( string, string) i get this error:
NullReferenceException: Object reference not set to an instance of an object
Register.Update () (at Assets/Scripts/Register.cs:32)
I don’t understand what is wrong?
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class Register : MonoBehaviour {
string CreateUserURL = "localhost/login.php";
public InputField username;
public InputField password;
public InputField confPassword;
public string username1;
public string password1;
public string confPassword1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)){
username1 = username.GetComponent<InputField>().text;
password1 = password.GetComponent<InputField>().text;
Debug.Log ("your username is " + username1);
CreateUser(username1, password1);
}
}
public void CreateUser(string username1, string password1){
WWWForm form = new WWWForm();
form.AddField("usernamePost", username1);
form.AddField("passwordPost", password1);
WWW www = new WWW(CreateUserURL, form);
Debug.Log("Done");
}
}
You need to learn how to solve this problem. Null exception errors are the most common errors… Is because some variable is not assigned, (is null) when trying to access it…
Now its happening in the line 32 as the ewrror says.
Use a Debug.Log or Debug the code while running and you will see some of the variables does not exist or are null (maybe username does not exist or does not have a component calles InputField)
Go spet by step, cheking everything and you will find by your own.
PD: As this is your 1st post, i will let it here, but we normally delete all posts about Null reference problems, because there are very simple to solve, and you have 10000 posts about the same.
Hello again!I followed your advice and the problem now is different.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
public class Register : MonoBehaviour {
string CreateUserURL = "localhost/users/login.php";
public InputField username;
public InputField password;
public InputField confPassword;
public string username1;
public string password1;
public string confPassword1;
// Use this for initialization
void Start () {
Debug.Log("Start");
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space)){
username1 = username.GetComponent<InputField>().text;
password1 = password.GetComponent<InputField>().text;
Debug.Log ("your username is " + username1);
CreateUser(username1, password1);
}
}
public void CreateUser(string username1, string password1){
WWWForm form = new WWWForm();
form.AddField("usernamePost", username1);
form.AddField("passwordPost", password1);
WWW www = new WWW(CreateUserURL, form);
Debug.Log("Done");
}
In unity i get the message ‘Done’ but in my database my records are increasing without the values of username and password (they are blank).
I can’t understand what’s wrong and any help would be usefull.