- Home /
Cycling Images...
I'm currently stuck trying to cycle images. I'm attempting to get so every time I press the mouse button it moves to the next "image". I tried doing...
function OnMouseDown(){
if (Input.GetMouseButtonDown(1));
guiTexture.texture = form2;
if (Input.GetMouseButtonDown(2));
guiTexture.texture = form3;
}
but it didn't really seem to work... It'll work when it's one but not two. I've tried a few different things and read plenty of posts but I couldn't seem to find anything that worked for what I'm aiming to do.
A example...
picture 1, picture 2, picture 3 mouse click 1 (loads picture 1), mouse click 2 (loads picture 2), mouse click 3 (loads picture 3), mouse click 4 (loads picture 1) and keeps letting you cycle.
Thanks in advance.
Answer by KiraSensei · Feb 23, 2014 at 01:49 PM
I'm quite sure you misunderstood the documentation about the mouse button HERE.
What I think you need is :
private var counter:int = 1;
function OnMouseDown(){
switch (counter)
{
case 1:
guiTexture.texture = form2;
break;
case 2:
guiTexture.texture = form3;
break;
case 3:
guiTexture.texture = form4;
break;
case 4:
guiTexture.texture = form1;
break;
}
++counter;
if (counter == 5) counter = 0;
}
You can also simplify this code with a list of textures, and acces to the right one with myTexturesList[counter]
.
That did it. Thank!!. I had tried the same formula but It was missing the actual counter portion of the it.
No problem. You just need to understand that the argument given to Get$$anonymous$$ouseButtonDown defines what button was used, not how many times it was used.
C#
int counter = 1;
void On$$anonymous$$ouseDown()
{
switch (counter)
{
case 1:
guiTexture.texture = form2;
break;
case 2:
guiTexture.texture = form3;
break;
case 3:
guiTexture.texture = form4;
break;
case 4:
guiTexture.texture = form1;
break;
}
counter++ ;
if (counter == 5)
counter = 0;
}
Answer by noporcru · Mar 26, 2014 at 06:28 PM
I'm trying to do something similar but am working in C#. I'm still fairly new to programming can someone please translate?
Ins$$anonymous$$d of posting an answer to ask something like this, use the "add new comment" button at the lower-right corner of an answer post. Thanks.
C# conversion posted above in the other answers comments.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134441im_/https://answers.unity.com/themes/thub/images/avi.jpg)