x


"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!

more ▼

asked Jul 01 '11 at 12:57 AM

Dylan Cristy gravatar image

Dylan Cristy
155 9 10 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 01 '11 at 01:53 AM

Peter G gravatar image

Peter G
15.1k 16 44 137

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...

Jul 01 '11 at 05:15 PM Dylan Cristy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4374
x336
x284
x5
x3

asked: Jul 01 '11 at 12:57 AM

Seen: 4244 times

Last Updated: Jul 01 '11 at 05:15 PM