I have a coroutine setup in UnityScript designed to move an object from one position to another. However, when calling the coroutine from another function in the same script nothing occurs.
This is the code i’m using:
function MoveDown (movObj : GameObject) {
Debug.Log("Coroutine Start");
Debug.Log(movObj.transform.position);
var elapsedTime : float = 0;
var time : float = 5;
var startPos : Vector3 = movObj.transform.position;
var endPos : Vector3 = Vector3(0,0.06,-4);
while (elapsedTime < time)
{
//Debug.Log("Moving...");
movObj.transform.position = Vector3.Lerp(startPos, endPos, Time.deltaTime);
elapsedTime += Time.deltaTime;
yield;
}
}
And I am calling it using:
MoveDown(object1go);
None of the Debug.Logs occur in the console and the object does not move. All of this code is inside of a custom class I have created and the object that i’m trying to move has been instantiated through code, if any of this is important.
What am I doing wrong that is causing it not to run properly?
By "inside a custom class" do you mean a class not inherited from MonoBehaviour - to be honest I'm not 100% sure, but I'd expect that the magic StartCoroutine() call Unity Script issues may only work on methods of a MonoBehaviour derivative. You could try calling StartCoroutine yourself... StartCoroutine(MoveDown(object1go));
– whydoidoitWhere are you calling it? Can you show the function where MoveDown is called?
– fafaseYes well that covers it then - you can't start a coroutine on something that isn't a MonoBehaviour - as this call is already indirect from a MonoBehaviour there's nothing to know that it's a coroutine. You would need a Behaviour subclass attached to the Unit1 prefab (one of your own scripts is good enough). Then you can do: object1go.GetComponent(MonoBehaviour).StartCoroutine(MoveDown(object1go));
– whydoidoit