how do I set an int to the scroll wheel data in unity

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#

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();

• 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. – TomTsagk Feb 6 at 17:54

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.

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