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

using my "potion" C#

Discussion in 'Scripting' started by amichael, Jun 20, 2016.

  1. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    i am trying to click my Potion button, and add 5 to my player health, and subtract 1 from my current potions.

    I have never done anything like this before but i am kind of stuck, and i might just be doing it all wrong, anyways here is what i have. Thanks in advance for the help.

    Code (CSharp):
    1. private void potionB_Click(object sender, System.EventArgs e)
    2.     {
    3.         UsePotion();
    4.     }
    5.    
    6.     public void UsePotion(int ptN = 1){
    7.         potionL -= ptN;
    8.         potionL -= 1;
    9.         playerL += 5;
    10.         potionText.text = potionL.ToString ();
    11.         playerText.text = playerL.ToString ();
    12.     }
    13.    
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Well, first thing I see is it appears you're subtracting 1 from potionL twice.
    Otherwise, I'm not sure what your question is. Are you getting a bug or is it just not updating?

    Is your potionB_Click tied to your button?
     
  3. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    Well it's not doing anything at all, no errors show up. I click and nothing happens , thanks for the reply.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  5. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    We add a check to see if we have any potions left, then exit out early if we have zero potions.
    Code (csharp):
    1. public void UsePotion() {
    2.  
    3.     // See if we are out of potions.
    4.     if (potionL <= 0) {
    5.         // No more potions. Do nothing.
    6.         return;
    7.     }
    8.  
    9.     potionL -= 1;
    10.     playerL += 5;
    11.     potionText.text = potionL.ToString ();
    12.     playerText.text = playerL.ToString ();
    13. }
     
  7. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    Thanks @Garth Smith i have another question, how could i add a random number generator to another button? i have this right now but it doesnt do anything..

    i want this button to subtract that random number from the healthbar...



    Code (CSharp):
    1.     private void attackB_Click(Button sender, System.EventArgs e)
    2.     {
    3.         Attack ();
    4.     }
    5.  
    6.  
    7.     public void Attack(){
    8.  
    9.  
    10.         {
    11.  
    12.             System.Random r = new System.Random ();
    13.             damageText.text = (r.Next (0, 10).ToString ());
    14.             numberGenerated();
    15.         }
    16.  
    17.     }