- Home /
 
How to get multiple string values from override ToString()?
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.
Answer by Chik3r · Apr 06, 2020 at 01:20 AM
You could make it return an array and select what you need. Code:
 public override string ToString()
 {
     string selectedName = this.name;
     string selectedCurrency = this.currency;
     return {selectedName, selectedCurrency}; // Return an array
 }
 countryText.text = country.PickRandomly().ToString()[0]; // To select the first item in the array
 
               Or pass an argument to ToString so it returns what you need.
 public override string ToString (string whatToReturn)
 {
     if (whatToReturn == "name") {
         return this.name;
     }
 
     if (whatToReturn == "currency") {
         return this.currency;
     }
 }
 
 countryText.text = country.PickRandomly().ToString("name"); 
 
              Your answer
 
             Follow this Question
Related Questions
Best way to store large quantities of information 2 Answers
Must I attach every script to a gameobject in order to work ? 2 Answers
Different language scripts - how to avoid trouble? 1 Answer
How to print a randomly selected string from Scriptable Objects? 1 Answer
Camera that follows rotating object with mouse input 0 Answers