- Home /
Elevator to load next scene
Hey guys, I'm having a big problem with my script. It does all what I want it to do until I put a yield on it so that it waits for the animation to finish before it loads the new scene; instead it plays no animation and just goes straight to a new scene. Script:
function Update ()
{
var hit :RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, 5))
{
if(hit.collider.gameObject.tag == "Elevator")
{
if(Input.GetKeyDown(KeyCode.I))
{
hit.collider.gameObject.animation.Play("Elevator");
StartCoroutine();
Startagain();
}
}
}
}
function StartCoroutine (){
animation.Play(animation.clip.name);
yield WaitForSeconds (animation.clip.length + 2);
}
function Startagain (){
Application.LoadLevel(1);
}
Thanks for your help Kanza
Your script has a few mistakes. I'll be glad to help if you format it properly first.
Hey man thanks for that, works perfectly now! I have a code which is working also but I'm unsure of many things, is there anyway I can contact you outside of here?
Thanks $$anonymous$$anza.
@$$anonymous$$arkFinn +1 for reading unformatted code. @$$anonymous$$anza Following the conventions actualy help a lot, I promise you.
Well this is my first time scripting and I'm bad at it soooo yeah sorry for it not being formatted.
Answer by MarkFinn · Dec 15, 2011 at 09:40 PM
MOve the call to StartAgain() inside the coroutine, after the yield.
That is,
function Update ()
{
var hit :RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit, 5))
{
if(hit.collider.gameObject.tag == "Elevator")
{
if(Input.GetKeyDown(KeyCode.I))
{
hit.collider.gameObject.animation.Play("Elevator");
StartCoroutine();
Startagain();
}
}
}
}
function StartCoroutine (){
animation.Play(animation.clip.name);
yield WaitForSeconds (animation.clip.length + 2);
Startagain();
}
function Startagain (){
Application.LoadLevel(1);
}
Yeah thanks works just dandy, now ima add a sound clip to it :P
Answer by MarkFinn · Dec 15, 2011 at 06:52 PM
As the system refuses to display my fully typed out answer, please accept this short form instead:
Simply move the call "Startagain();" inside the "StartCoroutine();", after the line "yield WaitForSeconds (animation.clip.length + 2);".