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 MidnightPride · Mar 07, 2012 at 12:16 PM · buttonforconvertinga

Changing the code from collision to button

Hello, I am working on a game ( kinda of but, i want to learn all the ins and outs of how to do things before starting ) This is something I have been wondering this is the code

I understand what the code does however I don't know the code to change from when the object collides with the floors to if a player presses a button

 function OnCollisionEnter (myCollision : Collision){
  If(myCollision.gameObject.name == "Floor"){
  Application.LoadLevel("Level01");
 }
 }
Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by PetterDK · Mar 08, 2012 at 07:52 AM

In that case what you're looking for is e.g. Input.GetKeyDown(KeyCode.F).

Documentation can be found here: http://unity3d.com/support/documentation/ScriptReference/Input.GetKeyDown.html

This function HAS to be called inside Update(), so implemented in your code it look like this:

 function Update () {
     if (Input.GetKeyDown (KeyCode.F))
         Application.LoadLevel(Level01);
 }

And another time, please don't post comments as an answer. Use the 'comment' option under each answer :)

Hope you succeed with your game..

Comment
Add comment · Show 1 · 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
avatar image MidnightPride · Mar 10, 2012 at 08:42 AM 0
Share

Thank you I appreciate it and, it was exactly what I was looking for!

avatar image
1

Answer by PetterDK · Mar 07, 2012 at 01:12 PM

First of all, I think your question is a bit unspecific.

If what you want to do is to load another level when the player presses a button in a GUI, you will have to use OnGUI(). Documentation for basic GUI scripting can be found here: http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

Then, your code would look something like this:

     function OnGUI () {
          if (GUI.Button (Rect (10,10,150,100), "Load another level..")) {
              Application.LoadLevel("Level01");
          } 
   }

If this is not what you want, then please specify further. What is the setup? Do you want to use a button in a GUI or a gameObject as the button?

Comment
Add comment · Show 1 · 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
avatar image MidnightPride · Mar 07, 2012 at 10:12 PM 0
Share

I'm sorry for not being specific enough. What I was looking for is when a player presses a $$anonymous$$eyboard button such as F and, that loads the new level. However thank you for posting I'm sure I will be using your example code reference.

avatar image
1

Answer by programmrzinc · Mar 07, 2012 at 01:14 PM

lets say you want to press a GUITexture:

 function OnMouseDown () {
 //Do some sexy stuff here
 }

You would ad this to the GUITexture or a 3DText, or even scripted GUIButtons.

Comment
Add comment · Show 1 · 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
avatar image PetterDK · Mar 07, 2012 at 01:50 PM 0
Share

This is what you would also use if you want to make a gameObject to be the button. Just use functions On$$anonymous$$ouseEnter, On$$anonymous$$ouseDown and the like, then throw it on the gameObject and you're good to go..

avatar image
1

Answer by Kleptomaniac · Mar 07, 2012 at 01:17 PM

Collisions are completely unrelated to Unity GUI. Use a GUI.Button. It returns a boolean = true when the button is pressed by a user.

Therefore, if you wanted to load "Level01" when a button is pressed, try this:

 function OnGUI () {
 if (GUI.Button(Rect(10,70,50,30),"Click me! I change the level!")) {
 Application.LoadLevel("Level01");
 }
 }

Too easy. :) Klep

Comment
Add comment · Show 2 · 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
avatar image PetterDK · Mar 07, 2012 at 01:22 PM 0
Share

Uhm, isn't this exactly what is already answered?

avatar image Kleptomaniac · Mar 07, 2012 at 08:24 PM 0
Share

Yeah sorry, my page hadn't refreshed when I wrote the answer ... :\ I hate that ... @$$anonymous$$idnightPride: PeterD$$anonymous$$'s answer is the one you want! He answered first! :)

avatar image
0

Answer by MidnightPride · Mar 08, 2012 at 12:13 AM

Thank you for all the responses and, they were helpful and, i will be keeping them in mind however I apologize for not being specific enough. I didn't mean if they pressed a GUI button but, rather a keep board button ( Such as F ) as a result the world would load a new scene.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I trigger an animation using a button? 3 Answers

Change Button GUIStyle on Click 1 Answer

Sliding Gui auto button generator... help 0 Answers

How do I make a button not interactable for a set amount of time after pressed? 0 Answers

my wave spawner not working properly 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