x


Left and Right Arrow Keypress for navigation between scenes.

Hi,

I need help urgently as i am new in unity3d.

i have 3 scenes, each scene has a left and right button.

I wanted this: If i am in scene1,and i press the right arrow button, it should do a Application.LoadLevel to scene 2. and if i press left arrow, i move back to previous scene.

But the strange thing is that, now with three scene setup and i go to scene 1 and when i press the right arrow, it moves me from Scene1 to scene2 to scene 3, without stopping at scene2. the left arrow bring me directly to scene 1 without stopping at scene2.

i tried forcing the variables KeypressedFw and KeypressedBck to false but seems does't work. please help.

here is the js for scene1, that i attached to my Main Camera.

function OnGUI() {

var keyPressedFw; var keyPressedBck;

keyPressedFw = false; keyPressedBck= false;

keyPressedFw = (Event.current.keyCode == KeyCode.RightArrow)? true : false; keyPressedBck= (Event.current.keyCode == KeyCode.LeftArrow)? true : false;

GUI.Button(Rect((Screen.width / 2) - 75, (Screen.height / 2) -50, 150, 100), "Scene 1"); GUI.Button(Rect((Screen.width / 2) - 150, (Screen.height / 2) -50, 50, 100), "Bck"); GUI.Button(Rect((Screen.width / 2) + 100, (Screen.height / 2) -50, 50, 100), "Fwds");

if (keyPressedBck == true) {
           }
if (keyPressedFw == true) {
    //  yield WaitForSeconds(.5);
     Application.LoadLevel ("scene2");
    }

} //onGui

Peviously i was using The following code that allows me to even use the mouse to change scenes:

function OnGUI() {

var keyPressedFw = (Event.current.keyCode == KeyCode.RightArrow)? true : false; var keyPressedBck= (Event.current.keyCode == KeyCode.LeftArrow)? true : false;

GUI.Button(Rect((Screen.width / 2) - 75, (Screen.height / 2) -50, 150, 100), "Scene 1");

if (keyPressedBck || GUI.Button(Rect((Screen.width / 2) - 150, (Screen.height / 2) -50, 50, 100), "Bck")) { } if (keyPressedFw || GUI.Button(Rect((Screen.width / 2) + 100, (Screen.height / 2) -50, 50, 100), "Fwds")) { // yield WaitForSeconds(.5); Application.LoadLevel ("scene2"); } }

please somebody help.

regards nigel

more ▼

asked Aug 24 '10 at 03:12 AM

Nigel gravatar image

Nigel
1 1 1 3

When you post code, highlight it all and press the button with 0's and 1's on it, and it will format nicely so it's easier to read.

Aug 24 '10 at 03:46 AM Marowi

Being new to Unity doesn't make your problems any more urgent - it just means you are likely to have more of them. What's with the empty ifs? I don't see anything about loading scene 3 anywhere in this code. Assuming only the yield is commented out in the second code, the only change is moving the buttons out of the if statements. Is this what you meant to post?

Aug 24 '10 at 04:47 AM skovacs1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

OnGUI may be called several times per frame. If you load another scene, its OnGUI will be called and if the code is about the same, the event will be fired quickly enough that it will register the arrow key and load the next scene. Try adding a timer or wait of some kind before you start checking for the user input.

more ▼

answered Aug 24 '10 at 04:49 AM

skovacs1 gravatar image

skovacs1
10k 11 25 91

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x100
x54

asked: Aug 24 '10 at 03:12 AM

Seen: 1647 times

Last Updated: Aug 24 '10 at 03:20 AM