Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by metallizard · Dec 11, 2013 at 12:37 PM · c#newbie

NullReferenceException: Object reference not set to an instance of an object

Hi, i got this error when i execute the script.

 NullReferenceException: Object reference not set to an instance of an object
 LoginGUI.LoginItems (Int32 windowID) (at Assets/Script/Login/LoginGUI.cs:47)
 UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:1395)

i don't really get it where it was wrong :(

so here's my code...

LoginGUI.cs

 using UnityEngine;
 using System.Collections;
 
 public class LoginGUI : MonoBehaviour 
 {
     string username = "";
     string password = "";
 //    string registerURL = "localhost/register.php";
 
     [HideInInspector]
     public string problem = "";
 
 
 
     LoginSystem loginSystem;
 
     // Use this for initialization
     void Start () 
     {
         Debug.Log("LoginGUI Initialized");
     }
 
     void OnGUI()
     {
         GUI.Window(0,new Rect(Screen.width/40, Screen.height / 40, Screen.width / 1.05f, Screen.height / 1.1f), LoginItems, "Login");
     }
     
     void LoginItems(int windowID)
     {
         GUI.Label(new Rect(Screen.width/ 5, Screen.height / 3.9f,375,30), "<-Username->");
         {
             username = GUI.TextField(new Rect(Screen.width/ 2.2f, Screen.height / 4, 375,30),username);
         }
 
         GUI.Label(new Rect(Screen.width/ 5, Screen.height / 2.15f,375,30), "<-Password->");
         {
             password = GUI.TextField(new Rect(Screen.width/ 2.2f, Screen.height / 2.2f,375,30), password);
         }
 
         if(GUI.Button(new Rect(Screen.width/ 5, Screen.height / 1.45f,Screen.width/ 7, Screen.height / 15), "Login"))
         {
             loginSystem.LoginWWW();
         }
 
         if(GUI.Button(new Rect(Screen.width/ 1.7f, Screen.height / 1.45f,Screen.width/ 7, Screen.height / 15), "Register"))
         {
             loginSystem.RegisterWWW();
         }
             
         GUI.Label (new Rect(Screen.width/ 2.3f, Screen.height / 1.85f,Screen.width/ 7, Screen.height / 15), problem);
     }
 
 
     public string getUsername()
     {
         return username;
     }
 
     public string getPassword()
     {
         return password;
     }
 
 
 }

 LoginSystem.cs

 using UnityEngine;
 using System.Collections;
 
 public class LoginSystem : MonoBehaviour 
 {
     LoginGUI loginGUI;
     string loginURL = "localhost/login.php";
     string registerURL = "localhost/register.php";
 
     
 
     // Use this for initialization
     void Start () 
     {
         Debug.Log("LoginSystem Initialized");
     }
 
     public void LoginWWW()
     {
         StartCoroutine(LoginHandler(loginGUI.getUsername(), loginGUI.getPassword()));
     }
 
     public void RegisterWWW()
     {
         StartCoroutine(RegisterHandler(loginGUI.getUsername(),loginGUI.getPassword()));
     }
 
     IEnumerator LoginHandler(string usernameH, string passwordH)
     {
         loginGUI.problem = "Checking username and password...";
         string loginURL_H = loginURL + "?username=" + usernameH + "&password=" + passwordH;
         WWW loginReader = new WWW(loginURL_H);
         yield return loginReader;
 
         if(loginReader.error != null)
         {
             loginGUI.problem = "Cannot locate the page ! ";
         }
         else
         {
             if(loginReader.text == "right")
             {
                 loginGUI.problem = "Logged in !";
             }
             else if(loginReader.text == "wrong")
             {
                 loginGUI.problem = "username / password wrong !";
             }
             else
             {
                 loginGUI.problem = "Internal error ! Contact the administrator !";
             }
         } StopCoroutine("LoginHandler");
     }
 
     IEnumerator RegisterHandler(string usernameH, string passwordH)
     {
         loginGUI.problem = "Registering..";
         string registerURL_H = registerURL + "?username=" + usernameH + "&password=" + passwordH;
         WWW registerReader = new WWW(registerURL_H);
         yield return registerReader;
         
         if(registerReader.error != null)
         {
             loginGUI.problem = "Cannot locate the page ! ";
         }
         else
         {
             if(registerReader.text == "registered")
             {
                 loginGUI.problem = "Registered !";
             }
             else
             {
                 loginGUI.problem = "Not registered ! if continue contact the administrator !";
             }
         } StopCoroutine("RegisterHandler");
     }
 
 }



i got the error in if(GUI.Button(new Rect(Screen.width/ 5, Screen.height / 1.45f,Screen.width/ 7, Screen.height / 15), "Login")) { loginSystem.LoginWWW(); }

anyone know the problem ? any clue is really help me :(

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
1

Answer by KellyThomas · Dec 11, 2013 at 01:19 PM

As the exception message states a Null Reference was found when running line 47:

 loginSystem.RegisterWWW();

It looks like to are trying to call a method of loginSystem but it is uninitialised i.e. has no instance of LoginSystem assigned to it.

The easiest way to fix that is to assign a value in the inspector. Alternatively you could find an instance programmatically by using `GetComponant()`.

Comment
Add comment · Show 2 · 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 metallizard · Dec 11, 2013 at 02:25 PM 0
Share

i have the function in LoginSystem public void RegisterWWW() { StartCoroutine(RegisterHandler(loginGUI.getUsername(),loginGUI.getPassword())); }

on LoginGUI.cs i am declaring it too

 LoginSystem loginSystem;


so what's wrong with my script then ?

avatar image KellyThomas · Dec 11, 2013 at 02:52 PM 0
Share
 LoginSystem loginSystem;

Declares a variable name and type but does not assign a value. At this point you have a variable that holds a value of null. You are unable to access any properties of a null variable, and if you try you will get a NullReferenceException.

So we need to assign a value to this variable before we can use it to do any work.

You will have the LoginGUI script attached to a GameObject in your scene. Use the inspector to look at this script and you will find a slot named "Login System" this is the inspector's representation of the loginSystem variable. If you also have the LoginSystem script attached to a GameObject in your scene you can 1) click the little circle to browse and select; or 2) drag and drop, to assign a value to this slot.

If your needs are more complex and this assignment can not be made at design time it can be done programmatically at run time. An example of how this is done can be found over here at this other answer.

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# Third Person Controller CS1061 0 Answers

AI help (C) 0 Answers

Problem with Camera Transition (First-person to third-person, with Slerp) 1 Answer

Whats a good place to learn UnityScript? 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