1
\$\begingroup\$

i need to set an int to data from the scroll wheel so I can have the script use this int to display an item attached to a number from that int. i'm using c#enter image description here

i set up my scroll wheel input, what am i doing wrong?

here is the script;

  counter = Mathf.RoundToInt(Input.GetAxis("ScrollWheel"));

    Items.text = counter.ToString();
\$\endgroup\$
  • \$\begingroup\$ Why do you use RoundToInt? I don't have experience with the ScrollWheel, but from having played with GetAxis it returns a value between 0 and 1, which RoundToInt will always convert to 0. \$\endgroup\$ – TomTsagk Feb 6 at 17:54
1
\$\begingroup\$

Mathf.RoundToInt() is not a conversion method, you must use a technique called casts.

Try converting from float to int:

int scrollWheel = (int) Input.GetAxis("ScrollWheel");

Tell me if it works.


To explain, by putting int at the start of the line the next word becomes a new int or integer. We then get the value of Input.GetAxis("ScrollWheel"), and use (int) to convert it from float to int so the types are compatible.

The reason Mathf.RoundToInt() will not work properly is because it ROUNDS the number to the nearest integer, as a math term, NOT the programming term. (see Integers - Math Reference) For example, if you have a number that is 0.5 and used RoundToInt(), it returns 1 (the closest integer.)

What you're looking for is a way to CONVERT from float to int. The above code is how to do so.

\$\endgroup\$
  • \$\begingroup\$ Can you elaborate on how this solves a problem with using Mathf.RoundToInt()? \$\endgroup\$ – DMGregory Feb 7 at 23:52
  • \$\begingroup\$ @DMGregory RoundToInt is a math method, not a conversion method. I made edits. \$\endgroup\$ – commandertuna Feb 9 at 20:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.