Counter with +/- capability?

Hi folks.

Today’s issue: Scripting a method of determining how many of a given type of units to build.

Here’s the code I have right now:

var infCobuildCounter = int;

function OnGUI () {
GUI.Box (Rect (80, 40, 100, 60), "Company");
	    		if (GUI.Button (Rect (80, 60, 20, 20), "-" + infCobuildCounter)) { 
					infCobuildCounter --;
				}
				if (GUI.Button (Rect (160, 60, 20, 20), "+" + infCobuildCounter)) { 
					infCobuildCounter ++;
				}
}

What this gives me is two buttons that look like this:

-0 +0

Which, since the integers move up and down together, is basically fine. What I’m looking for is a way of doing this:

  • 0 +

Where the integer goes up or down depending on if the plus or minus sign was clicked. I’ve tried it as GUI.Label, GUI.Box, etc but nothing seems to work.

Ideally this will be used to tell my factories/etc how many of each unit to build, so I know I’ll need to figure that side out next.

Not sure if this will work, but looks like your writing 2 of the infCobuildCounter with the GUI.Button( Rect…)). Try just drawing it once:

var infCobuildCounter = int;

function OnGUI () {
GUI.Box (Rect (80, 40, 100, 60), "Company");
	    		if (GUI.Button (Rect (80, 60, 20, 20), "-" + infCobuildCounter)) { 
					infCobuildCounter --;
				}
				if (GUI.Button (Rect (160, 60, 20, 20), "+")) { 
					infCobuildCounter ++;
				}
}

here it is in C# should be easy to convert I only added 1 line

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{

	public int infCobuildCounter;
	
	void OnGUI ()
	{
		GUI.Box (new Rect (80, 40, 100, 60), "Company");
		if (GUI.Button (new Rect (85, 65, 20, 20), "-")) {
			infCobuildCounter --;
		}
		if (GUI.Button (new Rect (155, 65, 20, 20), "+")) {
			infCobuildCounter ++;
		}
		GUI.Label(new Rect(130, 65, 30, 20),infCobuildCounter.ToString());
	}
}

I’ve never worked in C# before, what would I need to change to put that in JS? It looks almost entirely similar so I’m assuming the adjustment would be in these two lines

public int infCobuildCounter;

GUI.Label(new Rect(130, 65, 30, 20),infCobuildCounter.ToString());

just add this line to your script

GUI.Label(Rect(130, 65, 30, 20),infCobuildCounter.ToString());

And remove these from your GUI.Button lines
+ infCobuildCounter
- infCobuildCounter

You will have to alter the Rect() settings so its set out nicely.

Also
public int infCobuildCounter; is the C# version of var infCobuildCounter = int;

That’s about what I figured. Any idea if I can limit this with a range? Ideal situation would be for it to be limited to a range of 0-3, and every 4 units to add to the next column.

Ok I’ve got that set. I’m still trying to figure out if RangeAttribute would be useful, and the entry in the Script Reference isn’t particularly informative.

What I would like to do is limit these three variables

var infCobuildCounter : int; 
var infRegbuildCounter : int;
var infDivbuildCounter : int;

to a minimum of 0 (not able to go into the negatives) and a max of 3, with each 4th positive adding to the next column. So, for example, if infCobuildCounter = 3 and infRegbuildCounter = 0, clicking the “+” button on infCobuildCounter would reset that int to 0 and set infRegbuildCounter to 1.

I hope that makes sense.

You have already said how to do it by using if statements

if (GUI.Button (new Rect (155, 65, 20, 20), "+"))
{
infCobuildCounter ++;
if (infCobuildCounter > 3  infRegbuildCounter = 0)
{
 infCobuildCounter =0;
infRegbuildCounter = 1;
}
}

Awesome thanks. I didn’t know anything about the part.

If you want to continue to direct the overflow into the next column, perhaps a small change to the inner if block…

if (infCobuildCounter > 3)
{
   infCobuildCounter = 0;
   infRegbuildCounter++;
}

That’s perfect. Much appreciated guys.

Also to limit the minimum to 0 you can check the value in the GUI button

if (GUI.Button (Rect (85, 65, 20, 20), "-")  infCobuildCounter >0) {

            infCobuildCounter --;

        }

or something like that I cant think of the condition to check, it might be >=1

Part of the thrill of writing code is experimenting with various things and not knowing if it will work or not, so exciting :smile:

Equally balanced by wondering “Which part of that experiment didn’t work…”