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 PlayCreatively · Mar 24, 2016 at 09:41 PM · inputvariablecompare

Comparing a variable (The right answer) to user input.

I simply display a number on the screen that you're supposed to press on your keypad. The available buttons are 0 to 3. What is a good way to see if the user's input (numbers on the keypad) is the same number as displayed on screen?

I started using a switch statement but that just got cluttered with identical if statements, it worked but there's got to be a more nicely packaged way of doing this.

The number I'm displaying on screen is contained as an int in a list. So I'm comparing an int to the equivalent number on the keypad (input).

(I suggest that only one input should be able to be true at ones to minimize the off chance of getting it right with button mashing.)

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

2 Replies

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

Answer by Downstream · Mar 25, 2016 at 10:05 PM

Here's a script for getting started.

 using UnityEngine;
 
 public class RayCaster : MonoBehaviour{
     public int answer = 4;
     int numstart;
     public void Start(){
         numstart = (int)KeyCode.Alpha0;
     }
     bool anyNumberPressed = false;
     public void Update(){
         for (int i = 0; i < 10; i++) {
             if (Input.GetKeyDown ((KeyCode)numstart + i)) {
                 if (answer == i) {
                     Debug.Log ("YOU WIN");
                     anyNumberPressed = true;
                 } else {
                     Debug.Log ("YOU LOSE");
                     anyNumberPressed = true;
                 }
             }
         }
         if (anyNumberPressed) {
             answer = Random.Range (0, 9);
             anyNumberPressed = false;
         }
     }
 }
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 PlayCreatively · Mar 26, 2016 at 04:22 AM 1
Share

Oh wow. I didn't know you could so easily convert a variable's type like that! I have seen it done before, when instantiating gameObjects, but didn't realize I could use it in other contexts, this is truly an eye opener! I'm definitely gonna try to convert more types to open up more possibilities.

But to get to the point, this is exactly what I was talking about. I felt that repeating if statements was unnecessary and it turns out to be true. I also knew that I'd learn something from seeing another approach to the problem.

avatar image
0

Answer by DThaiPome · Mar 25, 2016 at 10:01 PM

All you really have to do is store the user's input in a variable, then use an if statement to check if the input is the same as the correct answer.

To store an input, you can either use the Input.GetKeyDown function, or you can set up an axis in the inputs menu in the project, then use the Input.GetAxis function.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Save Serial Port Data to a Variable? 2 Answers

How do I make a script that waits for player input or no player and then does something according to that? 0 Answers

Vibrating Xbox 360 controller without using Xinput? 0 Answers

Compare and sort Input 0 Answers

Object reference not set to an instance of an object (C#) 2 Answers


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