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 /
avatar image
0
Question by Rookiebatman · Mar 11, 2019 at 11:10 PM · rangeinteger

Is there a way to define a range of numbers in the place where an int would usually go?

I just discovered today that you can set Inputs to variable number keys with (Input.GetKeyDown(KeyCode.Alpha0 + [insert name of int here])). I have a list of bools (less than 10), and I wanted to do a thing where it would just be "whatever number key you press, that number item in the list of bools will be toggled." Basically, like this...

 if (Input.GetKeyDown(KeyCode.Alpha0 + ThisNumber)) 
         BoolList[ThisNumber] = !BoolList[ThisNumber];

Of course, the problem there is that "ThisNumber" has to be predefined. It doesn't just recognize any number key you press and plug that into the variable. Granted, it's easy enough to just say if (Input.GetKeyDown(KeyCode.Alpha1) {ThisNumber = 1}, and do that for each single-digit number. That probably would've even been easier than writing this post, but for the sake of general education, at least, is there a way to say something like this...

 if (Input.GetKeyDown(KeyCode.Alpha0 + AnyNumberBetweenOneAndNine)) 
          BoolList[ThatSameNumber] = !BoolList[ThatSameNumber];
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

Answer by Eno-Khaon · Mar 12, 2019 at 05:10 AM

For a simple and straightforward approach to this, you could run each input test individually in a for() loop:

 // C#
 
 // Cast KeyCodes as integers
 int alpha0Key = (int)KeyCode.Alpha0;
 int alpha9Key = (int)KeyCode.Alpha9;
 
 void Update()
 {
     // Use integer conversions as a value range to test
     for(int i = alpha0Key; i <= alpha9Key; i++)
     {
         // Cast the iterator as a KeyCode to return
         // to the intended range
         if(Input.GetKeyDown((KeyCode)i)
         {
             int arrayIndex = i - alpha0Key;
             BoolList[arrayIndex] = !BoolList[arrayIndex];
         }
     }
 }

Edit: Whoopsie, forgot to subtract a value to get the array indices into a sane range.

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
avatar image
0

Answer by fafase · Mar 12, 2019 at 07:13 AM

Here is a small code to retrieve the integer off the input (simplified version at the end):

 void Update ()
 {
     int n = Helper.GetDigitFromInput(Input.inputString); 
     if(n < 0) { return;}
     // use n
 }

 // Helper.cs
 public static int GetDigitFromInput(string str)
 {
     if (string.IsNullOrEmpty(str) == true) { return - 1; }
     int n = -1;
     if (int.TryParse(str, out n) == false) { return -1; }
     return n;
 }

I made the method static so it can be placed in a helper class. If no input, string is empty and return -1. then try to convert to integer and if fails, return -1. Check if -1 to figure out if number. then use n in your array as you were.

Simplified version:

 void Update ()
 {
     string str = Input.inputString;
     if (string.IsNullOrEmpty(str) == true) { return; }
     int n = -1;
     if (int.TryParse(str, out n) == false) { return; }
     // Use n
 }
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

101 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

Related Questions

newbie question how would get the percentage of its original starting value over its current value 1 Answer

Script so that, my enemies only see my player at a certain range. 4 Answers

The UVs of the detail mesh objects need to be in the 0-1 range? 2 Answers

How to know what random number is chosen 2 Answers

two Random.Range returning similar values 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