- Home /
Resouce script help
Ok, i have this script:
function OnGUI(){
if
(GUI.Button(Rect(90,10,150,40),"Work")){
print(Construction.Wood);
Construction.Wood += 10;
}
}
This mean that when i pres the button it add 10 Wood.
But is there posible to add 10 wood until I tell it to stop? (I want also to remain the button)
Answer by cncguy · Aug 07, 2010 at 12:04 AM
Perhaps something along the lines of:
var addingWood : boolean = false; //flag to set if we are adding wood
function OnGUI() { if(GUI.Button(Rect(90,10,150,40),"Work")) { if(!addingWood) { InvokeRepeating("addWood",0.0,1.0); // add wood each second addingWood = true; } } }
function Update() { if(someConditionToStopAddingWood()) { CancelInvoke("addWood"); addingWood = false; } }
function addWood() { print(Construction.Wood); Construction.Wood += 10; }
Ah, good thinking, he wouldn't want to produce wood every frame. You can combine this with the Toggle button by moving the stuff from Update() to OnGui: function OnGUI() { if(GUI.Toggle(Rect(90,10,150,40),false,"Work")) { [same as above] } else { if(addingwood){CancelInvoke("addWood"); addingWood = false;}}}
Yes thats true but it is likely that some other factor in his game stops the production of wood rather than the button I suspect.
Answer by Wolfram · Aug 06, 2010 at 11:36 PM
If you want something to happen repeatedly while the user holds down that button, use GUI.RepeatButton(). If you are looking for a toggle button (click once -> button stays enabled and wood is produced. click again -> button gets disabled), use GUI.Toggle().