Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 isiaaa · Jul 10, 2018 at 03:36 PM · databaseinputfieldregister

Pass input values to database,Pass my Input Fields to database

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");
     }
 }
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

2 Replies

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

Answer by tormentoarmagedoom · Jul 11, 2018 at 06:59 AM

Good day.

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.

so its in

 username1 = username.GetComponent<InputField>().text;

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.

Bye!

Comment
Add comment · 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
0

Answer by isiaaa · Jul 19, 2018 at 04:35 PM

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");
 }

} My php code is:

 <?php 
   $servername = "localhost";
   $server_username = "root";
   $server_password = "";
   $dbName = "myusers";
 
   $username = $_POST['usernamePost'];   
   $password = $_POST['passwordPost'];
 
 
   //Make Connection
   $conn = new mysqli($servername, $server_username, $server_password, $dbName);
   //Check Connection
   if(!$conn){
     die("Connection Failed.".mysqli_connect_error());
   }
   
   $sql = "INSERT INTO users(username, password)
           VALUES ('".$username."','".$password."')";
   $result = mysqli_query($conn, $sql);
  
   if(!$result) echo "there was an error";
   else echo "ok";
 
  ?>

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.

Thanks again!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

150 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 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 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 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 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

using databse for "enter name" system 0 Answers

Inventory that loads data from an XML 0 Answers

how to define a user id securely 0 Answers

Get Other Players information (those who are installed) ? 0 Answers

How to setup AWS dynamoDB that I can CRUD (create, read, update, delete) data by Unity? 0 Answers


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