How to change array index by clicking in java

I’m working on a script to cycle through an array of text slides. How can I use a loop to cycle through the array every time the mouse is clicked?

Hi there @BakedSteakGames

To loop through an array every-time a mouse button is clicked you would simply use the GetMouseButtonDown() function provided by Unity’s Input class. You can check for when the user has pressed this and then loop through the array. Consult the example below:

// Text slides array, doesn't matter what data type it is the principle is the same.
public var TextSlides: int[] = new int[5];

function Update () 
{
	// Checks if the user has pressed the left mouse button.
	if(Input.GetMouseButtonDown(0))
	{
		// Loop through the array.
		for(int i = 0; i < TextSlide.length; i++)
		{
			// Do whatever you need to do here.
		}
	}
}

For more info the GetMouseButtonDown() function please consult the API linked below:

GetMouseButtonDown - API

I hope this helps! :slight_smile: