Counting on mouse scroll

Hey, I am trying to get something to count when I move my mouse wheel. So when I move my mouse wheel foward it will count up and when I move it back it will count down. I also would like set the number limit from 1 to 5. Need the number of wheel turns to be in a variable so I can use it in if statements.

Thanks for help:)

using UnityEngine;
using System.Collections;

public class ScrollCounter : MonoBehaviour 
{

	public int whateverNumber = 1;
	
	// Update is called once per frame
	void Update () 
	{
		if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
		{
			whateverNumber = whateverNumber + 1;		
		}	 
	
		else if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
		{	
	    	whateverNumber = whateverNumber - 1;	
		}
	}
}

I think you can and should figure the rest out for yourself :slight_smile:

Thanks this works great but just need to add one thing, how would I get the min. number to 1 and the max number to 5?

Thanks:)

using UnityEngine;

using System.Collections;

public class ScrollCounter : MonoBehaviour

{

public int whateverNumber = 1;

// Update is called once per frame

void Update ()

{

if (Input.GetAxis(“Mouse ScrollWheel”) > 0 whateverNumber<5) // forward

{

whateverNumber = whateverNumber + 1;

}

else if (Input.GetAxis(“Mouse ScrollWheel”) < 0 whateverNumber>1) // back

{

whateverNumber = whateverNumber - 1;

}

}

}

or use Mathf.Clamp like

using UnityEngine;

using System.Collections;

 

public class ScrollCounter : MonoBehaviour 

{

 

    public int whateverNumber = 1;

    

    // Update is called once per frame

    void Update () 

    {

        if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward

        {

            whateverNumber = Mathf.Clamp(whateverNumber++, 1, 5);          

        }    

    

        else if (Input.GetAxis("Mouse ScrollWheel") < 0) // back

        {   

            whateverNumber = Mathf.Clamp(whateverNumber--, 1, 5);    

        }

    }

}

sort of thing

Thanks! Works great. :slight_smile: