Tutorial: iTween Missing Reference Exception after Application.LoadLevel

February 23, 2014
/ / /

A Unity C# tutorial on fixing missing reference exception errors thrown by iTween after using Application.LoadLevel.

Calling Application.LoadLevel in the middle of a tween will cause iTween to maintain the object references even though the objects attached to them are now gone. This causes the tweens array list to be incorrect, and iTween.Count() to be inaccurate.

To fix this issue, you must reset the array list manually. Create a new function inside iTween.cs to reset the tweens array by making a new list that is of the same type (you’ll find the type at the top of the file, where tweens is initially declared).

public static void Destructor()
    {
        // Deletes all references just before destruction
        // your file may have a different list type, such as
        // tweens = new ArrayList(); or; tweens = new List();
        tweens = new List<Dictionary<object, object>>();
    }

You can then call this function in your classes prior to using Application.LoadLevel. This will destroy the current list, allowing the newly loaded level to create a new list with proper references.

void GameOver()
    {
        iTween.Destructor();
        Application.LoadLevel(0);
    }

Alternatively, you could also call DontDestroyOnLoad() on each gameObject you tween.

One Comment

  1. Jamsi May 5, 2014 6:26 am

    Thanks for this workaround! This issue has bugged me for a while! Strangely it didn’t work when I tried to call the function tweens = new List() outside the iTween class. It only seems to work with the Destructor() call.

    Also I needed to call these 2 in the following order:
    iTween.Destructor();
    iTween.Stop();

    If either one was missing the MissingReferenceException came again.

Post a Comment