- Home /
Question by
joelfernandes23 · Apr 01, 2020 at 06:02 PM ·
c#scriptingbasicsscriptableobjectrandom.rangerandomize
How to print a randomly selected string from Scriptable Objects?
I want to randomly pick an item from a scriptable object and then print the randomly chosen item to the console.
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 Item[] m_Items;
}
How do I go on printing the following values to the console?
public Item PickRandomly()
{
int index = Random.Range(0, m_Items.Length);
return m_Items[index];
}
Comment
Best Answer
Answer by Roger_0123 · Apr 02, 2020 at 10:28 AM
Hello, you mean printing all the properties of the Item you retrieve from PickRandomly()? I don't think Unity has a built in method, because .toString()
returns only the name of he object (I think it will be "Item"
). You will need to build your own method:
public string StringifyItem(Item item){ return item.Name + " :{ currency: " + item.Currency + ", Capital: " + item.Capital + "}"; }