Start coroutines when the class is not a MonoBehaviour
I made a simple rig that moves stuff over time like this:
Transform from;
Transform to;
float overTime;
IUIAnimationEvent chain;
public delegate void UIchain();
public event UIchain NEXT_FUNCTION;
public MoveAction(Transform from, Transform to, float overTime, IUIAnimationEvent chain)
{
this.from = from;
this.to = to;
this.overTime = overTime;
this.chain = chain;
}
public void Move()
{
MonoBehaviour _lead = new MonoBehaviour();
if (moveRoutine != null)
{
_lead.StopCoroutine(moveRoutine);
}
moveRoutine = _Move(from, to, overTime);
_lead.StartCoroutine(moveRoutine);
}
IEnumerator _Move(Transform from, Transform to, float overTime)
{
Vector2 original = from.position;
float timer = 0.0f;
while (timer < overTime)
{
float step = Vector2.Distance(original, to.position) * (Time.deltaTime / overTime);
from.position = Vector2.MoveTowards(from.position, to.position, step);
timer += Time.deltaTime;
yield return null;
}
if(NEXT_FUNCTION != null)
{
NEXT_FUNCTION();
}
}
However, to make it work like I wish, I have to instantiate them
, so they can't be a MonoBehaviour
. Notice what I did with the _lead
variable. I made it so I could start coroutines
like any other. If my class is NOT a MonoBehaviour
, how to start a coroutine
from it? Or if that isn't possible, how to instantiate
classes that ARE MonoBehaviour
? I noticed the use AddComponent
instead, but the classes are not components. They are used by another component.
Your answer
Follow this Question
Related Questions
My components are disappears 1 Answer
Why isn't this instantiation cast working? 1 Answer
Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer
How to instantiate bullets in multiple transforms 1 Answer
Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers