Search Unity

Can I use a String Variable to Chose which Int I want to effect?

Discussion in 'Scripting' started by Raincolt, Jan 19, 2019.

  1. Raincolt

    Raincolt

    Joined:
    Jan 15, 2019
    Posts:
    13
    I have a button that depending on it's case will send a different string to a method, I did this with the expectation that when I get to the maths part Id have found a solution but didn't.

    basically I have like 40 int variables and a string variable that changes depending on user input.

    Can I use that String Variable to Chose which Int I want to effect.

    Code (CSharp):
    1.  
    2. public Button ClassOption1, ClassOption2, ClassOption3;
    3. int Comp, Eleg, Pres;
    4. public void ButtonOptions()
    5. {
    6.         Text Class1Text = ClassOption1.GetComponentInChildren<Text>();
    7.         Text Class2Text = ClassOption2.GetComponentInChildren<Text>();
    8.         Text Class3Text = ClassOption3.GetComponentInChildren<Text>();
    9. switch (CurrentButton)
    10.         {
    11.          
    12.             case "Button1Pressed":
    13.                 Class1Text.text = "Composure";
    14.                 Class2Text.text = "Elegance";
    15.                 Class3Text.text = "Presence";
    16.                 //Sends a string to Maths() method on button press
    17.                 ClassOption1.onClick.AddListener(() => Maths("Comp"));
    18.                 ClassOption2.onClick.AddListener(() => Maths("Eleg"));
    19.                 ClassOption3.onClick.AddListener(() => Maths("Pres"));
    20.                 break;
    21. }
    22.  
    23. public void Maths(string CurrentInt)
    24.     {
    25.    ///Summary
    26.    ///can I use the CurrentInt String to chose what variable to affect without using like 40 if
    27.   ///statements?
    28.  ///Summary
    29. int [insert Variable name here] +=1;
    30.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    If you want to convert a string to an int, you can do that with:

    Code (csharp):
    1. int i;
    2. if (int.TryParse( text, out i)) {
    3.   // conversion was successful
    4. }
    If you want to do math and put it back in a string, just use .ToString() on the integer to return a string!
     
  3. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    I wouldn't use string literals in this case, because it's more work to change things later. Also harder to debug if you have a typo somewhere. You could put your constants in a central location instead:
    Code (CSharp):
    1. public class ButtonType
    2. {
    3.     public const int COMP = 0;
    4.     public const int ELEG = 1;
    5.     public const int PRES = 2;
    6.     // etc
    7. }
    And always reference that.
    Code (CSharp):
    1. Dictionary<int, string> labels = new Dictionary<int, string>()
    2. {
    3.     { ButtonType.COMP, "Composure" },
    4.     { ButtonType.ELEG, "Elegance" },
    5.     { ButtonType.PRES, "Presence" }
    6. };