Search Unity

Sending values to menu - need simpler way

Discussion in 'Scripting' started by matthewr, Dec 22, 2014.

  1. matthewr

    matthewr

    Joined:
    Dec 9, 2014
    Posts:
    21
    My current layout looks like:

    Menu
    • Panel
    • Items (Button)
    • Special (Button)
    • Equipment (Button)
    • Formation (Button)
    • Save (Button)
    • CharOnePortrait
      • C1HP/MP (Text)
      • CharOneName (Text)
      • CharOneHP (Text)
      • CharOneMP (Text)
    • CharTwoPortrait
      • C2HP/MP (Text)
      • CharTwoName (Text)
      • CharTwoHP (Text)
      • CharTwoMP (Text)
    • CharThreePortrait
      • C3HP/MP (Text)
      • CharThreeName (Text)
      • CharThreeHP (Text)
      • CharThreeMP (Text)
    • Location1 (Text)
      • Location2 (Text)
    • Currency (Text)
    And I have a script attached to Menu - GameMenu.cs (C#) that I want to use to send values to all the Text entries on the menu.

    Well, I don't know how to do that, so I've been using GameObject.Find and putting in the names of the Text. Here is the code I've made, and it works, but it's so bloated.

    private Text charOneName;
    private Text charTwoName;
    private Text charThreeName;
    private Text charOneHP;
    private Text charTwoHP;
    private Text charThreeHP;
    private Text charOneMP;
    private Text charTwoMP;
    private Text charThreeMP;
    private Text currency;
    private Text location1;
    private Text location2;

    void Start ()
    {

    charOneName = GameObject.Find("CharOneName").GetComponent<Text>();
    charTwoName = GameObject.Find("CharTwoName").GetComponent<Text>();
    charThreeName = GameObject.Find("CharThreeName").GetComponent<Text>();
    charOneHP = GameObject.Find("CharOneHP").GetComponent<Text>();
    charTwoHP = GameObject.Find("CharTwoHP").GetComponent<Text>();
    charThreeHP = GameObject.Find("CharThreeHP").GetComponent<Text>();
    charOneMP = GameObject.Find("CharOneMP").GetComponent<Text>();
    charTwoMP = GameObject.Find("CharTwoMP").GetComponent<Text>();
    charThreeMP = GameObject.Find("CharThreeMP").GetComponent<Text>();
    currency = GameObject.Find("Currency").GetComponent<Text>();
    location1 = GameObject.Find("Location1").GetComponent<Text>();
    location2 = GameObject.Find("Location2").GetComponent<Text>();

    charOneName.text = "Matt";
    charTwoName.text = "Steven";
    charThreeName.text = "Ashley";
    charOneHP.text = "240" + "/" + "250";
    charTwoHP.text = "240" + "/" + "250";
    charThreeHP.text = "240" + "/" + "250";
    charOneMP.text = "29" + "/" + "35";
    charTwoMP.text = "29" + "/" + "35";
    charThreeMP.text = "29" + "/" + "35";
    currency.text = "$ " + "12.50";
    location1.text = "Twoson Town";
    location2.text = "Town";

    }

    There has to be a simpler way of doing this. I don't want to have to put a script on each text item.

    I would appreciate any help I receive. Thank you!
     
  2. Kirk Clawson

    Kirk Clawson

    Joined:
    Nov 4, 2014
    Posts:
    65
    You have all the code you need there. Just find the patterns and pull them out into reusable chunks. Here's something I came up with:
    Code (CSharp):
    1. void Start()
    2. {
    3.     SetTextComponent("CharOneName", "Matt");
    4.     SetTextComponent("CharTwoName", "Steven");
    5.     //...
    6. }
    7.  
    8. private void SetTextComponent(string componentName, string value)
    9. {
    10.     var current = GameObject.Find(componentName).GetComponent<Text>();
    11.     current.text = value;
    12. }
    If you need to actually keep a reference to a specific component (if it's something you'll update frequently for example), then it's simple enough to add a third optional ref-parameter to that method to fill in a field that you specify.
     
    matthewr likes this.
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    When you are dealing with things that are related to each other, then a class or struct makes it easier to take care of them.
    class Character{
    string name;
    string HP;
    string MP;
    }
    You can put a class inside another class or in another script.
    Then you can make an array of some type to keep the characters in and access them.
     
    matthewr likes this.