Coroutine calls from Class instances are BROKEN! (worked in 4.0)

So finally I have confirmed this major issue apparent in editions 4.1.2 and 4.1.5 after numerous experiments. In this example I will be using unityscript but 99% sure it exists in other languages as well.

Say you have a simple class(that has no base class).

You were able to to call a coroutine(any function that has yield keyword in it) from inside the class in version 4.0 and lower. But this new issue, whether it is a feature, a bug fix or a new bug is giving me a good headache. I am deliberately not extending the class from a MonoBehavior for a number of reasons.

I see no reasons why this new restriction is introduced. My project consists of tens of thousands of codes and I have been making use of this feature on a regular basis. My program is now ridden with irritating null reference exceptions over a version change.

Finally I would like to include the scripts to replicate this issue on your unity versions. (4.0 and 4.1.5) Just attach Dummy.js to a gameobject. This will print a “This is a coroutine.” message on 4.0. But will throw a null reference exception on 4.1.5. (if it works perfectly, that means the code needs a recompilation, just add an empty function in dummy.js) Furthermore, I wrote a wrapper function to illustrate how easily I can bypass the restriction. Just call wrapperForDummyCoroutine instead of dummyCoroutine inside callCoroutine and witness how it works.

I am amazed and at the same time sad that noone other than me has come across this issue… I hope Unity guys will give this thread the attention it deserves.

dummyClass.js

#pragma strict

public class dummyClass{
	
	public function dummyClass(){
	}
	
	public function callCoroutine(){
		dummy.use.dummyCoroutine();
	}
}

dummy.js

#pragma strict

static var use : dummy;

function Awake () {
	//----------------------------------------------------------------------------------------
    if (use) {
        Debug.LogWarning("Only one instance of the dummy script in a scene is allowed");
        return;
    }
    use = this;
    //----------------------------------------------------------------------------------------
}

function Start () {
	var dummyObj : dummyClass = new dummyClass();
	
	dummyObj.callCoroutine();
}

function dummyCoroutine(){

	yield;
	MonoBehaviour.print("This is a coroutine.");
	

}

function wrapperForDummyCoroutine(){

	dummyCoroutine();

}

Don’t forget to upload through Help > Report a bug, I think that is their main route for Bug Reports.

Thanks, will do.

I’m having a similar problem with code that worked in 4.0 and earlier that breaks in 4.1.5 except that it IS called from a monobehavior. I have a similar setup where I use a static variable in a controller to access it and the coroutine fails to continue. When I call the function on the static var, there is a coroutine that does a WaitForSeconds() My print statement before it prints out but the one after is never printed. I’ve checked the docs for changes but I can’t find anywhere that specifies why this broken in 4.1.x

I also did as you suggested and pushed the yield into a deeper function call and that fixes it. I wonder if it has something to do with static variables function calls in javascript?