Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

get gui.button to change value of int on click

Discussion in 'Scripting' started by louis66, Mar 11, 2015.

  1. louis66

    louis66

    Joined:
    Feb 5, 2015
    Posts:
    6
    so i have created a gui button through script and when i click that button i want it to add 10$ for example
    and i am coding in c#

    if(gui.button(some rect some location)){
    money +=10;
    }

    this is basically what i have and cant seem to get it to work but if i try and make it a bool and make it true and have an if statement in a different spot it does not work through the gui.button but if make pressing space bar change the bool to true it will add the 10$

    this does not work
    if(gui.button(some rect some location)){
    addmoney = true;
    }

    but this works
    if (input.getkeydown ("space")){
    addmoney = true;
    }

    void update{
    if (addmoney = true){
    money +=10;
    }
    }

    so after figuring out that i know the adding money part works fine but how come using the gui.button to set bool true does not work?
    and to make my button enabled i have a bool that needs to be changed to true and that works fine and if i click the button and have an ifstatement making the bool false it will disable the button. so i know the bool can be changed via gui button but i cant seem to find out why the add money bool wont work with the gui button.

    sorry for the bad grammar.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    Would be easier to just call a function on click:

    Code (CSharp):
    1.  
    2. if(gui.button(some rect some location)){
    3. AddMoney(10); // call this function to add the amount
    4. }
    5.  
    then have that function somewhere
    Code (CSharp):
    1.  
    2. void AddMoney(int amount)
    3. {
    4.  money+=amount; // adding is done here
    5. }
    6.  
     
  3. louis66

    louis66

    Joined:
    Feb 5, 2015
    Posts:
    6
    ive tried that and still does not work when i click on the gui button.