How to loop switch statements in Unity

Hi,

I’ve made a switch inside one of my GUI buttons. How do I set it up so that every click will increment the int value so it switched from case to case? And after it reaches 3 or more it’ll reset back to zero.

Here’s a snippet:

if(bgChange>4)
{bgchange = 1;}
	switch(bgChange++)
			{
				case 1:
					print("background02");
					break;
				
				case 2:
					print("background03");
				break;
				
				case 3:
					print("background04");
				break;	
				
				default:
					print("background01");
				break;
			}

At the moment, it goes up to 3 and back to 1 and it stops there. Sorry if this question sounds rush :stuck_out_tongue:

Thanks.
-Hakimo

that code looks more or less right, except for a typo: bgchange is not the same thing as bgChange :slight_smile: With that fixed, though, it probably still doesn’t do quite what you want; you’ll never see case 1, since you set bgChange to 1 then immediately increment it.

Try this:

bgChange = (bgChange + 1) % 4;
switch (cgChange) { … }

bgChange will vary from 0…3 and loop back to 0.

Hi Laurie,

Lol I’m embarrassed now :sweat_smile: but the typo was unintentional. I didn’t copy and paste my actual code :stuck_out_tongue:

Thanks very much by the way. I prefer your method which is cleaner than what I did last night. Basically, I used:

if(bgChange > 4) {
 bgChange = bgChange-4;}
 switch(bgChange++)
 ...

Thanks again.
-Hakimo