Hello,
contrary to most people I don’t have the problem of iTween not calling the oncomplete function at all: For me it is called twice in certain circumstances!
This seems to happen if I load a new level inside the oncomplete function.
Below is a code sample that illustrates the issue:
public class SmoothLevelChange : MonoBehaviour {
void OnTriggerEnter(Collider other) {
if(!other.tag.Equals("Player")) return;
GameObject.DontDestroyOnLoad(gameObject);
// Set GUITexture to cover the Screen
gameObject.GetComponent<GUITexture>().pixelInset = new Rect(Screen.width/2,Screen.height/2,0,0);
// fade GUITexture in
iTween.ColorTo(gameObject, iTween.Hash("a",1, "time",2, "oncomplete","oncomplete"));
}
void oncomplete() {
Debug.Log("oncomplete"); // prints twice
Application.LoadLevel("Scene2");
}
void OnLevelWasLoaded() {
Debug.Log("onlevelwasloaded"); // prints twice as well because oncomplete gets called twice
// ... some setup stuff like positioning the player ...
// ... and fade back in ...
//GameObject.Destroy(gameObject);
}
}
The way I see it is that iTween gets somehow ‘restarted’ when the new Level loads (there is a noticeable delay between the first and second call of oncomplete).
If you remove the Application.LoadLevel from the oncomplete function it gets called only once (like it should in the first place).
What am I missing here or is it really a bug?
And more importantly, how can I fix this behaviour?
I want to delete the object after the level was loaded, but I can’t because if I do, I’ll get an error saying that iTween tries to call a function on a non existing object.
Or is there a better way of fading the screen to black, change the level and then fade the new level back in?