- Home /
iTween fires oncomplete twice if LoadLevel in oncomplete
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?
ok, seems like I found my error: after loading the new level, my player collides again with the trigger. therefore the whole event loop is started all over again.
since the link provided by dannyskim is really usefull (thanks for it) I'll mark his answer as correct.
Answer by dannyskim · Feb 15, 2012 at 09:17 PM
Not quite sure why that's happening. But as far as an alternative, you may want to look at:
http://www.unifycommunity.com/wiki/index.php?title=FadeInOut
The above link provides fully functional camera fading utilizing OnGUI and a one-pixel repeated texture tiled across the screen. So, you can use black, or white, up to you.
The one that I prefer to use is ratmat2000's version about 3/4 of the way down the page. Remember, OnGUI uses render texture and is very inefficient and expensive, so if you're targeting mobile platforms I would suggest you cache the script as a variable and enable / disable appropriately after utilizing it.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
itween: firing oncomplete AFTER ONE LOOP 0 Answers
iTween oncomplete method on normal C# class 1 Answer
How to write a simple save script 1 Answer
iTween & Boo 1 Answer