Search Unity

finish animation before executing loadlevel function

Discussion in 'Scripting' started by soeren96, Aug 30, 2014.

  1. soeren96

    soeren96

    Joined:
    Feb 7, 2014
    Posts:
    3
    I really hope some of you can help me out here.
    How can I prevent the loadlevel function to execute before the animation has ended.
    My code is here:

    usingUnityEngine;
    usingSystem.Collections;



    public class LoadNewScene :SceneFadeInOut{
    boolfinish = false;
    stringsceneName;
    publicstringbackground = "";

    void OnMouseDown(){
    sceneName = this.gameObject.name;
    print (sceneName);
    GameObject.Find(background).animation.Play("FadeOutBeforeNew");

    Application.LoadLevel(sceneName);



    }


    }
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  3. soeren96

    soeren96

    Joined:
    Feb 7, 2014
    Posts:
    3
    Thanks.
    I tried "isPlaying", but could'nt make it work.
    But instead I used the "Invoke" method and it works. Now the newScene waits 2 seconds before executing.

    void On MouseDown(){
    sceneName = this.gameObject.name;
    GameObject.Find(background).animation.Play ("FadeOutBeforeNew");
    Invoke ("newScene", 2); // waits 2 seconds before executing the method

    }
    void new Scene(){

    Application.LoadLevel(sceneName);

    }
    }