- Home /
Instantiate ScriptableObject with a random asset from List
Hey,
I'm new at ScriptablObjects ("SO") so my question might be basic.
I have a Patient SO. This Patient contains another SO wich is a Disease SO.
I'd like to generate a Patient with a random Disease picked from a List of Diseases (like a database).
Here's some pseudocode :
public class emergencyRoom : MonoBehaviour {
public List<Patient> patients;
public List<Disease> diseases;
public int availableRoomsNumber = 6;
void Start () {
//here I'd like to fetch all the assets made from Disease SO,
// something like :
//diseases = AssetDatabase.Get........
for(int i = 1; i < availableRoomsNumber; i++)
{
//here I create a new Patient instance from Patient SO, and put it in the list of patients.
Patient newPatient = ScriptableObject.CreateInstance<Patient>();
patients.Add(newPatient);
//assign a random disease from the SO disease list.
patient.Disease = disease[Random.Range(0, disease.Count)]
}
Thank you very much for your help, I am really looking forward to mastering SO
Answer by TreyH · Feb 01, 2018 at 06:28 PM
ScriptableObjects need to be instantiated:
Patient newPatient = ScriptableObject.CreateInstance<Patient>();
Haha thank you, I just figured that out 2 $$anonymous$$utes ago, But still I don't know how to create a list of all the assets made from the Disease ScriptableObject. They all are located in a DiseasesAssets folder.
Do you know how to do that? Thank you
Are you able to put that folder into a / the Resources folder? If so, then off the top of my head you'd do something like:
Object[] loadedDiseases = Resources.LoadAll("DiseasesAssets");
for (int k=0; k < loadedDiseases.Length; k++)
{
this.diseases.Add( loadedDiseases[k] as Disease);
}
That would probably work, thank you, I see in the guidelines it's advised to use AssetBundles ins$$anonymous$$d. I'll read more about that first. Thank you !!
Your answer
Follow this Question
Related Questions
ScriptableObject.CreateInstance(Type T) keep crashing 2 Answers
Reference to scene objects from scriptable objects 1 Answer
ScriptableObject reference resetted after a method is finished. 2 Answers
ScriptableObject not saving data to asset 2 Answers
How to Instantiate objects that are Scriptable Objects 0 Answers