0
\$\begingroup\$

I want to get multiple string values(selectedNameand selectedCurrency) individually from public override string ToString()below. How do I go on about it?

    using System.Collections;
       using System.Collections.Generic;
       using UnityEngine;  

       [CreateAssetMenu(fileName = "Country", menuName = "Country/Country", order = 0)]
       public class country : ScriptableObject
       {
           [System.Serializable]
           public class Item
           {
               public string Name;
               public string Currency;
               public string Capital;
               public string[] City;

    public override string ToString()
    {
    string selectedName = this.name;
    string selectedCurrency = this.currency;
    return selectedName + selectedCurrency; 
    }

           }

           public Item[] m_Items;
       public Item PickRandomly()
    {
        int index = Random.Range(0, m_Items.Length);
        return m_Items[index];
    }
}

I need to use the selected string values in the script below.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class GameController: MonoBehaviour
    {   public country country; //This is the scriptable object country
    public Text countryText;
    public Text currencyText;

    void Start(){
    countryText.text = country.PickRandomly().ToString(); // This is where I need the country name string to show up.
currencyText.text = country.PickRandomly().ToString(); // This is where I need the currency string to show up.
    }
    }

Right now it just displays a random country name and currency for example USA USD in both the fields.

\$\endgroup\$
  • 3
    \$\begingroup\$ You know you can make your own string-returning functions like GetCountryName() and GetCurrencyName()? You don't have to use ToString() for this at all. \$\endgroup\$ – DMGregory Apr 6 at 3:29
  • \$\begingroup\$ @DMGregory Thanks. That worked. :) \$\endgroup\$ – Joel Fernandes Apr 6 at 15:14
0
\$\begingroup\$

There are many ways of implementing what you are after.
As @DMGregory said you could write your own separate methods for getting the country and the currency:

[System.Serializable]
public class Item
{
    public string Name;
    public string Currency;
    public string Capital;
    public string[] City;

    public string GetCurrency()
    {
        return Currency;
    }

    public string GetName()
    {
        return Name;
    }
}

Alternatively, you can just access the fields of Item directly:

void Start(){
    countryText.text = country.PickRandomly().Name;
    currencyText.text = country.PickRandomly().Currency;
}

PS: If you want to do more advanced stuff with the data without having to implement lots of methods to get strings (such as if you wanted to be able to get country name and currency as one string) I would suggest you have a look using properties: Using Properties - C# Programming Guide | Microsoft Docs

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks. That worked. :) \$\endgroup\$ – Joel Fernandes Apr 6 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.