Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by laura11 · Mar 16, 2017 at 08:34 PM · buttonbeginnerif-statements

How to combine a if statement with whether or not a certain button was clicked?

Hello there, we have a project for university and i am totally new to unity. We have to create a street map with buttons on it. In the quiz mode, a question is shown in the dialogue panel. The player then has to select the right button that answers the question right. e.g. "It is raining and you don't want to walk to the city. Where do you go?" The right answer would then be to click on the bus button. After three trials, the wright button would start flashing.

I posted a picture for better understanding. It is in German - sorry for that!

My question now is: How do i create a script, that
1. prints "Great" in the dialogue Panel if the right button is clicked (here it would be the bus button)
2. Prints "Try again" if a wrong button is clicked
3. know when to flash the right button (after three misses). alt text Thanks so much! I tried so many ways but it just won't work.

bildschirmfoto-2017-03-16-um-143708.png (491.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Matt1000 · Mar 16, 2017 at 09:09 PM

Buttons are themselves if statements, you can actually do this:

 public void OnGUI () {
     if (GUI.Button (new Rect (position, size), texture)) {
         //Code
     }
 }

If you need to check if the answer is right i would recommend using an enum. You should create an enum with all your possible buttons/answers and then have a variable which iis the right one:

     public enum Places {
         BusStation,
         Hospital,
         Hotel,
         //Etc.
     }
 
     Places rightAnswer = Places.BusStation;
     Places answer;
 
     public void OnGUI () {
         //Bus Station Button
         if (GUI.Button (new Rect (position, size), busStationTexture)) {
             answer = Places.BusStation;
             if (answer == rightAnswer) {
                 //Great!
             } else {
                 //Try Again
             }
         }
         //Bus Hospital
         if (GUI.Button (new Rect (position1, size), hospitalTexture)) {
             answer = Places.Hospital;
             if (answer == rightAnswer) {
                 //Great!
             } else {
                 //Try Again
             }
         }
         //Bus Hotel
         if (GUI.Button (new Rect (position2, size), hotelTexture)) {
             answer = Places.Hotel;
             if (answer == rightAnswer) {
                 //Great!
             } else {
                 //Try Again
             }
         }
     }

Surely not the nicest way to do it but that's the idea :)

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

106 People are following this question.

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

Related Questions

Change Camera Position when pressing a button whilst changing scenes 1 Answer

Going back to MainMenu from other level. (Problem) 0 Answers

Im trying to make gameObject move only if specific button is pressed 1 Answer

Play audio once while condition is met in update function (C#) 2 Answers

How to write a script for an existing button 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