- Home /
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!
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.
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?
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.
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; }
}
}