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 cherlick · Apr 14, 2014 at 05:20 PM · public staticstatic script

NullReferenceException: on Static variables

Hi, Can please some one help me? I'm trying something i saw in Unity Live training but is not working in my script and i cant find why.

this is Script1:

 using UnityEngine;
 using System.Collections;
 
 public class TesrMor : MonoBehaviour {
 
     public static TesrMor tM;
     public int n = 5;
     public bool mn=false;
 
     void Update()
     {
         Debug.Log(n);
     }
 }

and this script2

 using UnityEngine;
 using System.Collections;
 
 public class testget : MonoBehaviour {
 
     int u=20;
     bool q=true;
     // Use this for initialization
     void Start () 
     {
         u = TesrMor.tM.n;
     }
     
     // Update is called once per frame
     void Update () 
     {
         q=TesrMor.tM.mn;
 
 
     }
 }

all the time give me this error : NullReferenceException: Object reference not set to an instance of an object testget.Start () (at Assets/Scripts/Player/testget.cs:11) and NullReferenceException: Object reference not set to an instance of an object testget.Update () (at Assets/Scripts/Player/testget.cs:17)

Can some one help me ??

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 nicolasjr · Apr 14, 2014 at 05:22 PM 0
Share

You're not setting any value for that variable.

You'll need to initialize it or to drag a gameobject to the variable field on inspector.

avatar image nicolasjr · Apr 14, 2014 at 05:23 PM 0
Share

Is your intention to do a Singleton?

avatar image cherlick · Apr 14, 2014 at 05:27 PM 0
Share

Yes is that singleton. at least i think so

avatar image cherlick · Apr 14, 2014 at 05:52 PM 0
Share

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

This is what i want to do. but is not working

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nicolasjr · Apr 14, 2014 at 05:34 PM

In order to do a Singleton, you need some extra code:

 public class TesrMor : MonoBehaviour {
  
     TesrMor() { }
 
     public static TesrMor tM
     {
         get
         {
             if(_tM == null)
             {
                 _tM = new TesrMor();
             }
             return _tM;
         }
     }
     public int n = 5;
     public bool mn=false;
  
     private static TesrMor _tM;
 
     void Update()
     {
        Debug.Log(n);
     }
 }

Let me know if it worked!

Comment
Add comment · Show 1 · 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 nicolasjr · Apr 14, 2014 at 06:00 PM 0
Share

oh, by the way, you'll need to set the private pointer to the class itself once you initialize it!

 void Start()
     {
         _t$$anonymous$$ = this;
     }

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

21 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

Related Questions

How do I access a boolean from another class in c#? 1 Answer

How to Find Job Unity 3d Desainer 2 Answers

Why is Unity deleting my prefabs!? 1 Answer

2D Border doesnt allow the nutbomb gameobject to bounce 0 Answers

Object reference not set to an instance of an object 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