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();
}