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 Sejemus · Sep 08, 2014 at 08:33 PM · c#nullreferenceexceptionfunction

NullReferenceException on function call

Hello unity community, I know this question is asked alot and I really have tried to find the answer I'm looking for, but I just don't know what to search for or where to look. I just can't get away from this exception:

NullReferenceException: Object reference not set to an instance of an object LoginScript+c__Iterator2.MoveNext () (at Assets/Scripts/LoginScript.cs:91)

All needed variables are public.

All classes have the following applied:

 public static ClassName Instance
     {
         get
         {
             return _instance;
         }
     }
     private static ClassName _instance;


This is the function that gives me the exception:

 IEnumerator login(WWW w)
     {
         message += "Logging in..";
         yield return w;
         if (w.error == null)
         {
             if (w.text == "Success")
             {
                 loggedin = true;
                 message = "";
                 status = "Online";
                 AppManager.Instance.UpdateStatus(); <-- This is where I get the null reference exception
             }
             else
             {
                 message += w.text;
             }
         }
         else
         {
             message += "ERROR: " + w.error + "\n";
         }
     }


The UpdateStatus() function looks like this:

 public void UpdateStatus()
     {
         WWWForm updateform = new WWWForm();
         updateform.AddField("action", "updatestatus");
         updateform.AddField("token", LoginScript.Instance.token);
         updateform.AddField("user", LoginScript.Instance.user);
         updateform.AddField("status", LoginScript.Instance.status);
         updateform.AddField("pos", "");
         WWW ww = new WWW(LoginScript.Instance.usermanagerpage, updateform);
         StartCoroutine(SendDataToDatabase(ww));
     }

Thanks in advance!

Comment
Add comment · Show 4
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 rutter · Sep 08, 2014 at 06:54 PM 0
Share

The error message is in LoginScript, which makes me think that App$$anonymous$$anager.Instance is null. Have you checked for that?

If you're not sure what that means, you should look up some tutorials that explain what null and NullReferenceException are.

avatar image Sejemus · Sep 08, 2014 at 07:16 PM 0
Share

I have this piece of code in the App$$anonymous$$anager class though?

 void Start()
     {
         if(_instance != this)
         {
             if(_instance == null)
             {
                 _instance = this;
             }
         }
     }
avatar image rutter · Sep 08, 2014 at 08:40 PM 0
Share

Can you guarantee that gets called before LoginScript does its thing? $$anonymous$$any singletons use a "lazy instantiation" pattern, where they're created on-demand the first time they're needed. This is a little bit more coding, but it makes sure the Whatever$$anonymous$$anager is always available.

avatar image Sejemus · Sep 08, 2014 at 09:03 PM 0
Share

I do the code above in initialization, before I define the variables, so it's the very first thing that gets defined.

1 Reply

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

Answer by dsada · Sep 08, 2014 at 08:48 PM

Try to make the singleton class like this:

 public class AnyClass : MonoBehaviour
 {
      private static AnyClass _instance;
      
      public static AnyClass GetInstance()
      {
          if(_instance == null)
          {
              GameObject obj = new GameObject();
              _instance = obj.AddComponent<AnyClass>();
          }
           
           return _instance;
      }
 }

also it is pretty much the same if the singleton class doensnt have to be a MonoBehaviour:

 public class AnyClass
 {
    private static AnyClass _instance;
 
    public static AnyClass GetInstance()
    {
        if(_instance == null)
        {
            _instance = new AnyClass();
        }
        
        return _instance;
    }
 
 }

In both cases you can use it anywhere like this:

 AnyClass.GetInstance().DoWhatever();

And with this strategy you can be absolutetly sure that the instance is not null when you are using it.

Comment
Add comment · Show 4 · 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 Sejemus · Sep 08, 2014 at 09:20 PM 0
Share

This seems to have solved the NullReferenceException. Thanks! I don't quite understand why the method I used didn't work, can anyone explain that to me, that would be awesome :) But it runs without further NullReferenceExceptions for now :D

avatar image dsada · Sep 09, 2014 at 02:47 PM 0
Share

Because you either didnt give a value for your _instance variable at all or the App$$anonymous$$anager.Instance.UpdateStatus(); line was running before you gave a value to it.

avatar image Sejemus · Sep 09, 2014 at 05:17 PM 0
Share

Why does it have to create a new game object? Can't it read the script that is already in the scene? Or can't the scripts access scripts attached to the camera?

avatar image dsada · Sep 09, 2014 at 07:48 PM 0
Share

It is not neccessary to create a new GameObject. If you have a GameObject to attach you can use that of course. Since i would have liked to write a short code this seemed O$$anonymous$$ to me.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Null Reference Exception in Custom Function 1 Answer

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

My function doesn't appears in the OnClick button script 5 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