Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nigel · Aug 24, 2010 at 03:12 AM · navigationkeypress

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Aug 24, 2010 at 03:46 AM 1
Share

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.

avatar image skovacs1 · Aug 24, 2010 at 04:47 AM 0
Share

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. Assu$$anonymous$$g 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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by skovacs1 · Aug 24, 2010 at 04:49 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to use explicit navigation without arrow keys? 0 Answers

proximity, keypress and mouse lock help please :) 1 Answer

Basic animation key press 0 Answers

How To Activate/Deactivate Image Effect On Keypress? 3 Answers

Need to press key twice to react 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges