- Home /
troubles with changing scenes
hellooo, i'm having a little problem with switching scenes on my game, what im doing is trying to make a playable tutorial, there are some visuals on the screen telling you to press 'a' and i want it so that when you press 'a' , the game waits 3 seconds before loading the next scene, here is the code i've made and there are no errors but the game simply does not move to the next screen.
#pragma strict
function Start () {
if (Input.GetKeyDown("a"))//pressing the a key loads the game level,
{
yield WaitForSeconds (3);
Application.LoadLevel ("tutorial2");
}
}
function Update () {
}
Note that you've put your code in the Start method. This method is called once when the script component is enabled. You probably want this code to go in Update ins$$anonymous$$d.
Answer by brockk · Jan 12, 2015 at 10:34 AM
Hi Chrissyprice ,
use this code to solve problem
function Update () {
if (Input.GetKeyDown("a")) {
changescene ();
}
}
function changescene () { yield WaitForSeconds(3); Application.LoadLevel ("tutorial2"); }
this code worked perfect cheers man, could i ask also how i would apply this if i had to press 'a' and left shift at once?
what i guessed isnt right if (Input.Get$$anonymous$$eyDown("a")) && (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift){ changescene (); }
Hi Chrissyprice ,
function Update () {
if (Input.Get$$anonymous$$eyDown("a")&&Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))//pressing the a key loads the game level, {
changescene ();
}
} function changescene () { yield WaitForSeconds(3); Application.LoadLevel (1); }
pls accept answer
happy to help you
Answer by Josh Naylor · Jan 12, 2015 at 10:26 AM
Check out the documentation for a function called Invoke
Invoke( "Load", 5 );
function Load(){
Application.LoadLevel(...);
}
This calls the method "Load" after 5 seconds.
Answer by Bunny83 · Jan 12, 2015 at 10:36 AM
Start is executed only once, so your if statement is only checked once at start. You have to do that check in Update and your delayed level loading in a seperate coroutine:
static var waitForLevelLoad = false;
function Update ()
{
if (Input.GetKeyDown("a"))
{
LoadLevelDelayed("tutorial2", 3);
}
}
function LoadLevelDelayed(levelName, delay)
{
if (waitForLevelLoad)
yield break;
waitForLevelLoad = true;
yield WaitForSeconds (delay);
waitForLevelLoad = false;
Application.LoadLevel(levelName);
}
The boolean "waitForLevelLoad" prevents the user from "queueing" multiple loadlevels at the same time.
Your answer
Follow this Question
Related Questions
Noob - Use the ESC, Shift and Command Key?? 1 Answer
GUI Label & KeyCode Doesn't Work 1 Answer
Is there a way to get precise input ? 1 Answer
No keyboard events detected after any key is held. 0 Answers
Input Key named: LeftAlt is unknown 2 Answers