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 darkmwar · May 29, 2014 at 09:54 AM · c#errorstaticmethodscompile-error

Both static and non-static method is giving me an error

I have a globalVariable.cs

      public class globalScript : MonoBehaviour {....
            public static int getFreeQueue(){
            return queue.Dequeue();
     }
           }

and I call it from spawn.cs

public class spawn : MonoBehaviour {

            void Start () {......
               int index = globalScript.getFreeQueue();
                .....
                    }

That gives me a run time error:NullReferenceException: Object reference not set to an instance of an object globalScript.getFreeQueue () (at Assets/globalScript.cs:23) spawn.Start () (at Assets/spawn.cs:17)

If I change getFreeQueue to non-static, then:

public class globalScript : MonoBehaviour {....

            public int getFreeQueue(){
            return queue.Dequeue();
     }
           }

public class Base : MonoBehaviour {

 int index ;    
 void Start () {
     index = globalScript.getFreeQueue();        
 }

}

It gives me a compile error: Assets/Base.cs(8,38): error CS0120: An object reference is required to access non-static member `globalScript.getFreeQueue()'

It seems like I run in to errors weather it is static or not. Any advices and suggestions would be greatly appreciated!

Comment
Add comment · Show 2
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 Owen-Reynolds · May 29, 2014 at 11:44 AM 1
Share

One simple thing might be to never use static. You never need it, especially in Unity. Everything goes on some gameObject, so the "finding scripts on other objects" trick.

avatar image Catlard · Jun 05, 2014 at 10:19 AM 0
Share

Hi, Owen! Good to see you again. Although I disagree -- I think static methods are important, especially when you're taking things from one project to anther, where you'll need to just pop things in and not want to switch back to the editor , wait for it to compile, and then drag and drop every time you want to link up some other component to get to your maths class. Do you have some better way of doing this that I doni't know about?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · May 29, 2014 at 09:55 AM

Static methods can only access static members. Your queue is probably non- static, make it static and it will probably work.

In the second case you then need an object of the class to use non static method.

All in all, you need to read about static and you will understand better your error.

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 darkmwar · May 29, 2014 at 11:12 AM 0
Share

ahh thanks so much. I will try it.

avatar image
1

Answer by Catlard · Jun 05, 2014 at 04:38 AM

If you want a static monobehavior, you should try a script I use all the time -- SingletonMonobehavior. It's below. You can place only one in a scene (place more than one, and you'll get errors). Then you can access any part of it globally with MyClassName.instance.FunctionName(). To declare a class of SingletonMonoBehavior that you want to access globally in your scene, you can just write something like this: public class MyClass : SingletonMonoBehavior {}. Then you can access that script from any other script in the scene, without having a reference directly to it, or having to assign it in the inspector. Hope it helps!

 using UnityEngine;
 using System.Collections;
 
     public abstract class SingletonMonoBehavior<T> : MonoBehaviour
     {
         
         private static T _instance;
     
         public virtual void Awake() {
     
             if(_instance == null || _instance.Equals(default(T)))
                 _instance = (T)((System.Object)this);
         }
         
         public static T instance {
             get { return _instance; }
         }
         
     }


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

22 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

Related Questions

Displaying a static variable from another script with OnGUI 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

help me please dont understand problem with animation 0 Answers

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