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

How can I make random buttons need help

Discussion in 'Scripting' started by makelchs, Mar 10, 2022.

  1. makelchs

    makelchs

    Joined:
    Feb 4, 2022
    Posts:
    12
    Hello I'm making three button which gives upgrade when you click
    and type of upgrade is randomly chosen when level up


    So when level gets up button name will randomly change
    Code (CSharp):
    1.     public void LevelUp()
    2.     {      
    3.             for (int i = 0; i < 3; i++)
    4.             {
    5.                 RandomDraw();
    6.                 if (i == 0)
    7.                 {
    8.                     b1text.text = b;
    9.                 }
    10.                 else if (i == 1)
    11.                 {
    12.                     b2text.text = b;
    13.                 }
    14.                 else if (i == 2)
    15.                 {
    16.                     b3text.text = b;
    17.                 }
    18.             }
    19.     }
    and each button have script like this
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         b = ThisText.text;
    4.         switch (b)
    5.         {
    6.             case "Add Gun Damage":
    7.  
    8.                 B1.onClick.AddListener(GunDamageUp);
    9.                 b = "null";
    10.                 break;
    11.             case "GunAUTO":
    12.  
    13.                 B1.onClick.AddListener(GunAuto);
    14.                 b = "null";
    15.                 break;
    16.             case "GunATS":
    17.  
    18.                 B1.onClick.AddListener(GunSpeedUp);
    19.                 b = "null";
    20.                 break;
    21.            
    22.  
    23.         }
    24.     }
    so when I level up button text will change and script will put onClick one
    is add gun damage then if I click button it increases damage
    It works fine in first level up but when I get more level, button dosen't work properly
    It sometimes make wrong result (text says dmg up but when I click speed is up)
    or get multiple upgrade (text says dmg up but dmg up + speed up)
    I have no idea how to fix it :(

    Plz help me to fix this problem
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    What happens in RandomDraw()?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You want to get away from this type of thing.

    To the greatest extent possible, always treat all Unity properties and fields as write-only, especially if they are text.

    Not only that but you are parsing this in Update() which means every single frame you are adding the listener... again and again and again... each one a separate listener connection.

    In just one second you would have added 60 listeners, so clicking upgrade button ONCE will bang that delegate 60 times... or THOUSANDS of times.

    Instead, structure your program so that you:

    - add a set of listeners ONCE (usually in OnEnable())

    - remove those listeners ONCE (usually in OnDisable())

    - the listener should ONLY connect to the button at a very abstract level (eg, "button 1")

    - you decide in code what that means (button 1 means Gun damage up)

    - when you receive button 1 you decide "I must do Gun Damage up" and then you can ask:

    --> is that legal right now?

    --> if not, display something saying "NOPE"

    --> if yes, then up the gun damage and display "DONE"

    Note that only the first add / remove steps touch the button itself. Everything else is completely disconnected from Unity and doesn't care. You could hook in a cheat key as well if you want, no other logic changes.
     
  4. makelchs

    makelchs

    Joined:
    Feb 4, 2022
    Posts:
    12
    It's just random function that pick text to change :)
     
  5. makelchs

    makelchs

    Joined:
    Feb 4, 2022
    Posts:
    12
    Thanks a lot! I'll try it