EDIT: The answer that solves this problem is indeed in the answer of the post whose duplicate is this one. If you have the same problem as in the title, please refer to the other post.
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 and will not be put on a GameObject
from the inspector.