- Home /
"Object reference required" error when using Start/StopCoroutine
Hi,
I am trying to learn some C#, and to that end I made this script to practice implementing a few things (properties, events, general C# syntax).
The script:
using UnityEngine;
using System.Collections;
public class Timer : MonoBehaviour
{
private static float timeLeft;
private static bool isTiming = false;
public static float TimeLeft
{
get { return timeLeft; }
set { timeLeft = value; }
}
public delegate void CountdownEndedHandler ();
public static event CountdownEndedHandler CountdownEnded;
public static void CountDownFrom (float countdownTime)
{
if (isTiming) return;
else
{
Debug.Log("CountDownFrom " + countdownTime);
isTiming = true;
timeLeft = countdownTime;
StartCoroutine(Countdown(countdownTime));
}
}
private static IEnumerator Countdown (float countdownTime)
{
Debug.Log("Firing Countdown()");
if (timeLeft <= 0) EndCountdown();
else
{
yield return new WaitForFixedUpdate();
timeLeft -= Time.deltaTime;
Debug.Log("timeleft = " + timeLeft);
}
}
private static void EndCountdown ()
{
StopAllCoroutines();
isTiming = false;
timeLeft = 0f;
if (CountdownEnded != null) CountdownEnded();
}
}
However, I am getting the following errors:
Assets/Scripts/Timer.cs(28,25): error CS0120: An object reference is required to access non-static member `UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)'
Assets/Scripts/Timer.cs(46,17): error CS0120: An object reference is required to access non-static member `UnityEngine.MonoBehaviour.StopAllCoroutines()'
Part of my objective in this exercise is to create a class that has methods that can be called from other scripts, without this particular class having any instance of itself (i.e., the script lives only in my 'Scripts' folder, and is not attached to any GO), which is why everything is set to 'static'. (That's my understanding of what 'static' does -- makes things available on a more global level as opposed to from an individual instance of something.)
Can anyone help me understand what I'm doing wrong / missing here?
Thanks!
Answer by Peter G · Jul 01, 2011 at 01:53 AM
I believe you need an instance to call StartCoroutine()
given that StartCoroutine()
is an instance method and a static method doesn't have an instance. Normally you can solve that issue with a singleton:
public class Timer : MonoBehaviour {
private static Timer instance;
public static Timer Instance {
get {
if(instance == null) {
var gB = new GameObject("Timer");
instance = gB.AddComponent<Timer>();
}
return instance;
}
}
}
then you just use Instance that you can call all the methods from. You might need to rework some code, but it isn't to hard to fix:
public void Update() {
Timer.Instance.StartCoroutine("Time");
}
And a small note on something else you said. static
doesn't make a field global because a private static var
is also legal. It is a little different than C/C++, but the static keyword associates the field with the class, not its instances. I think you understand the concept, but there is a difference that sometimes confuses users.
Thanks... in the end I did not make the "Timer" class a singleton, but I did have to make another class of which I make an instance in order to run the countdown coroutine...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Couroutines not working in static class 1 Answer
Where is the Mistake in my Coin System? 1 Answer
Why isn't my simple coroutine working? (and how can I make it infinite?) 2 Answers