- Home /
Coroutine gets skipped in javascript/unityscript
The class is instantiated in a different script:
sunRotation = new AutoRotate(sunContainer, dayLength * 60, Vector3.up);
, the constructor calls the coroutine and nothing happens. Debugging shows coroutine being called, it goes over to the line where the coroutine starts, then jumps back to the constructor and finishes executing never returning to the coroutine or even running anything inside it.
I tried putting the coroutine inside of the class code. I tried making it private/public. Even after looking at tutorials dealing with classes and coroutines and reading through the forums I'm still unable to see what I'm doing wrong. :(
This is the watered down version of the code (if you need more information, look below for the ENTIRE CLASS code):
#pragma strict
public class AutoRotate {
public function AutoRotate (parameters) {
//Calls the coroutine
RotateCoroutine(parameters); //Goes to the coroutine and jumps back
//Continues here
}
}
function RotateCoroutine (parameters) { //This line executes and then it jumps back to the origin point
while (something) { //This line NEVER GETS REACHED
//code
yield WaitForSeconds(someFloatVariable);
}
//This line NEVER GETS REACHED either
}
This is the whole class code here, nothing held back:
#pragma strict
public class AutoRotate {
private var priority : float = 0.25;
public function set Priority (value : float) {
priority = value; //Assets/AutoRotate.js(6,28): BCW0028: WARNING: Implicit downcast from 'Object' to 'float'.
if (priority < 0.0) {
Debug.Log("AutoRotate > 'priority' can't be '" + priority + "'! Corrected to 0.25!");
priority = 0.25;
}
}
public var isActive : boolean = true;
public function AutoRotate (object : GameObject) {
AutoRotate (object.transform, 4, Vector3.up);
}
public function AutoRotate (object : Transform) {
AutoRotate (object, 4, Vector3.up);
}
public function AutoRotate (object : GameObject, rotationTime : float, rotationAxis : Vector3) {
AutoRotate (object.transform, rotationTime, rotationAxis);
}
public function AutoRotate (object : Transform, rotationTime : float, rotationAxis : Vector3) {
RotateCoroutine(object, rotationTime, rotationAxis);
}
}
function RotateCoroutine (object : Transform, rotationTime : float, rotationAxis : Vector3) {
while (isActive) {
if (!object) Debug.Log("AutoRotate > RotateCoroutine > 'object' is 'NULL'.");
Debug.Log(rotationAxis);
object.transform.Rotate(rotationAxis, 360 / priority / rotationTime);
yield WaitForSeconds(priority);
}
}
I don't believe you can have a Coroutine in a class not derived from $$anonymous$$onobehaviour. As a test, make the class derived from $$anonymous$$onobehaviour and then dynamically create a game object and attach the script with AddComponent().
Thanks :D It works. There were more errors afterwards but I got it working. However, I'm not quite content with the way I did it. I might just try to implement a factory function ins$$anonymous$$d... maaaaaaybe.
Anyways, thanks a lot. I've been trying to tackle it for hours on end :D
Your answer
Follow this Question
Related Questions
yield works, but yield WaitForSeconds does not? 1 Answer
Question about Coroutines in a Class Function 1 Answer
C# to UnityScript conversion help. 0 Answers
Updating a TextMesh buggy? 1 Answer
Stuck while solving a Coroutine problem 0 Answers