- Home /
Null Reference Exception after updating from 4.0 to 4.1.5 (coroutines)
UPDATE
I replicated the issue below in an empty project, confirmed it and posted it here.
I have a piece of code that works perfectly on Unity 4.0, however after I updated my unity version to 4.1.5, it is not working properly anymore.(it is also broken on 4.1.2) Precisely, I am getting a null reference exception while attemping to call a function which is a coroutine, whose type is IEnumerator. I made sure that all parameters are valid and I can access the function.
This function is being called from a javascript object.(a class instance of a GO) And the function is on a seperate object.
function showSimpleEffect ( c : GameObject, effectName : String, duration : float, scale : Vector3 ) : IEnumerator{
var simpleEffect : GameObject = Instantiate( simpleEffectPlanePrefab , c.transform.position + Vector3(0,0.4,0) , simpleEffectPlanePrefab.transform.rotation );
simpleEffect.transform.localScale = scale;
simpleEffect.renderer.materials[0].SetTexture("_MainTex", Resources.Load("simpleSfxTextures/" + effectName ));
simpleEffect.transform.parent = c.transform;
var rate = 1.0/duration;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * rate;
if( t <= 0.4 ){
simpleEffect.renderer.material.SetColor ( "_Color", Color( Mathf.Lerp( 0.5, 0.5, t*2.5 ) , Mathf.Lerp( 0.5, 0.5, t*2.5 ), Mathf.Lerp( 0.5, 0.5, t*2.5 ) , Mathf.Lerp( 0.0, 1.0, t*2.5 ) ) );
}else if( t >= 0.6 ) {
simpleEffect.renderer.material.SetColor ( "_Color", Color( Mathf.Lerp( 0.5, 0.5, (t-0.6)*2.5 ) , Mathf.Lerp( 0.5, 0.5, (t-0.6)*2.5 ),Mathf.Lerp( 0.5, 0.5, (t-0.6)*2.5 ), Mathf.Lerp( 1.0, 0.0, (t-0.6)*2.5 ) ) );
}
yield;
}
Destroy(simpleEffect);
}
This is a very simple function that shows an image above a gameobject for a number of seconds. I have to point out that if I remove the while loop, the function works properly. (Probably because it is not a coroutine anymore) I don't know what the developers of Unity changed after 4.0 but I can't figure it out by myself, any help or clue is appreciated.
NullReferenceException
Effect.onActivate (Boolean applyResult) (at Assets/Scripts/cardFunctions.js:2098)
Effect.setActiveness (Boolean applyResult) (at Assets/Scripts/cardFunctions.js:1849)
Effect.onAdd () (at Assets/Scripts/cardFunctions.js:2540)
cardFunctions.addEffect (UnityEngine.GameObject onWhichCard, .Effect effectToAdd) (at Assets/Scripts/cardFunctions.js:2690)
cardProperties.onPlay () (at Assets/Scripts/cardProperties.js:2887)
mastermind+$moveCardToSlotEnemy$1164+$.MoveNext () (at Assets/Scripts/mastermind.js:1538)
Thanks in advance.
$$anonymous$$aybe shared$$anonymous$$aterial ins$$anonymous$$d of material? Your errors don't seem to point to your code above btw.
$$anonymous$$aybe this? (http://docs.unity3d.com/Documentation/ScriptReference/index.Writing_Scripts_in_Csharp_26_Boo.html[1] ) Coroutines have a different syntax in C#. Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; ins$$anonymous$$d of just yield ... ;.
@$$anonymous$$ajos We are sure that the function doesn't even get called at run time when yield is used, it is not related to materials. This function works perfectly in Unity 4.0
@Guidez We are using UnityScript, not C# or boo.
If you comment out the "if" block in the while block, does it still fail?
Your answer
Follow this Question
Related Questions
C# yield not doing anything 2 Answers
How to use yield within a class function 2 Answers
problem with using classes and coroutines 1 Answer
Yield Wait for Seconds (A little help here) 2 Answers
Need to call yield TWICE ??? (ANSWERED) 3 Answers