Hi All,
I am showing a page increment counter on a GUI. The code below doesn’t work properly:
@script ExecuteInEditMode
var message = “Instructions”;
var counter1: int;
var counter2 : int;
var pagecount : int;
function OnGUI () {
GUI.Label(Rect(120, 20, 140, 25), message+" "+pagecount);
if (GUI.Button(Rect(320, 20, 70, 15), “Next”)) {
pagecount ++;
}
if (GUI.Button(Rect(10, 20, 70, 15), “Previous”)) {
pagecount --;
}
print("Counters: " + counter1 + ", " + counter2 + ", " + pagecount);
}
while this code DOES!!
@script ExecuteInEditMode
var message = “Instructions”;
var counter1: int;
var counter2 : int;
var pagecount : int;
function OnGUI () {
GUI.Label(Rect(120, 20, 140, 25), message+" "+pagecount);
if (GUI.Button(Rect(320, 20, 70, 15), “Next”)) {
pagecount ++;
counter1 ++;
}
if (GUI.Button(Rect(10, 20, 70, 15), “Previous”)) {
pagecount --;
counter2 --;
}
print("Counters: " + counter1 + ", " + counter2 + ", " + pagecount);
}
Can someone explain to me why? It appears in the first example that there are 2 instances of pagecount, one for each button as when you press the button an appropriate number of times pagecount then continues to increment or decrement. But why does adding the unique counter variable to each button allow pagecount to work as a single instance (i.e as expected)?
With thanks, Peter