- Home /
Question by
joelfernandes23 · Mar 31, 2020 at 03:22 PM ·
c#scriptableobjectrandom.range
Random string from scriptable objects
Hi, I am trying to implement a country feature. I have imported the countries in a Scriptable Object. how do I randomly pick up an Item using Random.Range?
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;
}
Comment
Best Answer
Answer by Menubarbe · Mar 31, 2020 at 03:33 PM
Hi, you can pick randomly an Item index, like that :
public Item PickRandomly()
{
int index = Random.Range(0, m_Items.Length);
return m_Items[index];
}
Thanks for your answer. How do I print that selected value to the console using Debug.Log?
I tried this.
days = country.PickRandomly().ToString();
and
days = country.m_Items[index].ToString();
but I still get the errors.
events.Start ()NullReferenceException: Object reference not set to an instance of an object