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

Pass by reference on button clicks C#

Discussion in 'Scripting' started by wolfpack4417, Jun 2, 2015.

  1. wolfpack4417

    wolfpack4417

    Joined:
    Jul 5, 2014
    Posts:
    8
    Hey guys I have a quick question that I can't figure out.

    I want to combine the following methods into one method

    Code (CSharp):
    1. public void OnHealthButtonClick()
    2.     {
    3.         hpModifier++;
    4.         unusedSkillPoints--;
    5.  
    6.         if (unusedSkillPoints == 0)
    7.         {
    8.             skillPanelReadyButton.interactable = true;
    9.         }
    10.     }
    11.  
    12.     public void OnAttackSpeedButtonClick()
    13.     {
    14.         attackSpeedModifier++;
    15.         unusedSkillPoints--;
    16.  
    17.         if (unusedSkillPoints == 0)
    18.         {
    19.             skillPanelReadyButton.interactable = true;
    20.         }
    21.     }
    22.  
    23.     public void OnDamageButtonClick()
    24.     {
    25.         damageModifier++;
    26.         unusedSkillPoints--;
    27.  
    28.         if (unusedSkillPoints == 0)
    29.         {
    30.             skillPanelReadyButton.interactable = true;
    31.         }
    32.     }
    33.  
    34.     public void OnMoveSpeedButtonClick()
    35.     {
    36.         moveSpeedModifier++;
    37.         unusedSkillPoints--;
    38.  
    39.         if (unusedSkillPoints == 0)
    40.         {
    41.             skillPanelReadyButton.interactable = true;
    42.         }
    43.     }
    And I want them to basically be this method that I can put on each button's OnClick section in the inspector.

    Code (CSharp):
    1. public void OnStatButtonClick(ref int modifier)
    2.     {
    3.         modifier++;
    4.         unusedSkillPoints--;
    5.  
    6.         if (unusedSkillPoints == 0)
    7.         {
    8.             skillPanelReadyButton.interactable = true;
    9.         }
    10.     }
    Then I want to just set which modifier to change in the inspector. Is this possible?

    Thanks,
    Tucker
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    UnityEvents can't have parameters that are references to primitives like that, no.
     
  3. Deleted User

    Deleted User

    Guest

    One thing you could do is have a dictionary with an enum key and an int value.

    Then you could have your method but it would simply take an enum and not by reference.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You could always wrap the int in its own class that could be passed by reference. I believe this is called boxing.

    Reflection is also a possibility, if you need ultimate flexibility.
     
  5. wolfpack4417

    wolfpack4417

    Joined:
    Jul 5, 2014
    Posts:
    8
    OK thanks for the help and suggestions guys.