- Home /
Use Delay time in Abstract Class
Hello !! I am a study that try to create game in unity 3D. I have a problem .my question is "I can't to use: Startcouratince(functionname()); for delay time in abstract : public classname PlayState : IStateBase {} but i only can use in normal class Class name:MonoBehaviour {} "
Dear sir/$$anonymous$$adam
Thank you for this answer. But I need to make Delay time in Abstract Class. Do you have other way to make Delay time for using in abstract class?
Answer by fafase · Oct 28, 2014 at 08:35 AM
Your abstract class can inherits from MonoBehaviour, IStateBase is probably an interface so:
public abstract class PlayState:MonoBehaviour, IStateBase{}
If IStatebase is also a class then add MonoBehaviour at some point of the inheritance.
Answer by Bunny83 · Oct 28, 2014 at 05:59 AM
Coroutines run on GameObjects / MonoBehaviours. That's why StartCoroutine is a method of the MonoBehaviour class. You can define coroutines in custom classes, but you have to use a MonoBehaviour component to start (and run) the coroutine.
I've written a singleton class called CoroutineHelper which has some useful methods to run coroutines or even OnGUI code from wherever you want (except other threads ;)).
In your case all you need is the singleton property of the class. So you could simply use a helper like this:
public class SimpleCoroutine : MonoBehaviour
{
private static SimpleCoroutine m_Instance = null;
public static SimpleCoroutine Instance
{
get
{
if (m_Instance == null) {
m_Instance = (new GameObject("SimpleCoroutine")).AddComponent<SimpleCoroutine>();
DontDestroyOnLoad(m_Instance.gameObject);
}
return m_Instance;
}
}
}
You don't need to attach this script to any object manually. Just use it like this:
SimpleCoroutine.Instance.StartCoroutine(YourCoroutine());
That's all.
This works. Under the hood coroutines are not that difficult. You can implement a system to run them yourself by calling IEnumerator.$$anonymous$$oveNext.
Dear sir/$$anonymous$$adam
Thank you for this answer. But I need to make Delay time in Abstract Class. Do you have other way to make Delay time for using in abstract class?
@reaksmey-rt: Uhm, as i said this works no matter from what class you execute it. An abstract class is just a base class. From an "abstract class" itself you can't do anything because you can't have an instance of an abstract class. So you probably have a derived class which is not abstract. It doesn't matter if you start the coroutine with my approach from code that is in the abstract class or in the derived class. It will work (again: If you execute this on the main thread as Unity is not thread safe).
You will for sure get no better answer without more details. Again the fact that you have an abstract class somewhere is no reason at all. You provide only general and abstract information, we can only answer with general information. If you want a specific answer, ask a specific question and include the required information.
If you don't want to post your class(es) here that's fine, but then post an equivalent example that shows what you do and where you want to do what.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170127im_/https://answers.unity.com/themes/thub/images/avi.jpg)