- Home /
Couroutines not working in static class
This is giving me the following error, is there any workaround for this?
'Assets/timeManager.cs(28,17): error CS0103: The name`StartCoroutine' does not exist in the current context'
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public static class timeManager {
private static float originalFixedDelta = Time.fixedDeltaTime;
#region Internal Coroutines
private static IEnumerator SlowMotionCurveCouroutine(float minTimeScale, float totalTime)
{
yield return null;
}
#endregion
public static void SetTimescale(float newTimescale)
{
Time.timeScale = newTimescale;
Time.fixedDeltaTime = newTimescale * originalFixedDelta;
}
public static void SlowMotionCurve(float minTimeScale, float totalTime)
{
StartCoroutine(SlowMotionCurve(minTimeScale, totalTime));
}
}
Answer by Julien-Lynge · Jun 22, 2013 at 07:40 PM
Coroutines derive from MonoBehavior (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html)
This means that your class must similarly derive from MonoBehaviour, and the coroutine must be managed by an instance of a MonoBehaviour-derived class. In other words, you cannot start a coroutine from a static class. You could, for instance, define a singleton MonoBehaviour class to manage your couroutines, and then just call MyClass.Instance.StartCoroutine, and you can pass this a static IEnumerator. People have also built classes to wrap around and manage coroutines from static classes. This is the one I happen to use: http://forum.unity3d.com/threads/94220-A-more-flexible-coroutine-interface
Your answer
Follow this Question
Related Questions
[C#] Is there a way to call static variables directly, independent from parent class? 3 Answers
C# yield not doing anything 2 Answers
If there is only 1 instance of a class, should it be Static? - C# 2 Answers
"Object reference required" error when using Start/StopCoroutine 1 Answer
Multiple Cars not working 1 Answer