x


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)

more ▼

asked Aug 06 '10 at 09:57 PM

Supremfire gravatar image

Supremfire
31 8 10 18

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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;
}
more ▼

answered Aug 07 '10 at 12:04 AM

cncguy gravatar image

cncguy
1k 21 25 36

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;}}}

Aug 07 '10 at 12:16 AM Wolfram

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.

Aug 07 '10 at 12:32 AM cncguy
(comments are locked)
10|3000 characters needed characters left

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().

more ▼

answered Aug 06 '10 at 11:36 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

(comments are locked)
10|3000 characters needed characters left

thanks a lot! it is very helpfull

more ▼

answered Aug 07 '10 at 06:54 AM

Supremfire gravatar image

Supremfire
31 8 10 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x155
x100
x20
x10

asked: Aug 06 '10 at 09:57 PM

Seen: 395 times

Last Updated: Aug 06 '10 at 10:03 PM