Null Reference Exception after updating from 4.0 to 4.1.5 (coroutines)


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




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.

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.

more ▼

asked Jun 26, 2013 at 04:45 PM

hydrow gravatar image

16 3 2 4

Maybe sharedMaterial instead of material? Your errors don't seem to point to your code above btw.

Jun 27, 2013 at 09:01 AM Kajos

Maybe this? (http://docs.unity3d.com/Documentation/ScriptReference/index.WritingScriptsinCsharp26_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 ... ; instead of just yield ... ;.

Jun 27, 2013 at 02:37 PM Guidez

@Kajos 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.

Jun 27, 2013 at 04:03 PM hydrow

If you comment out the "if" block in the while block, does it still fail?

Jun 27, 2013 at 04:50 PM Guidez

yes, it fails.

Jun 27, 2013 at 04:56 PM hydrow
show all comments (comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 26, 2013 at 04:45 PM

Seen: 723 times

Last Updated: Jun 27, 2013 at 10:03 PM