Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mrsev · Feb 01, 2018 at 06:20 PM · scripting problemrandomscriptableobjectassetdatabasescriptable object

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TreyH · Feb 01, 2018 at 06:28 PM

ScriptableObjects need to be instantiated:

 Patient newPatient = ScriptableObject.CreateInstance<Patient>();
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrsev · Feb 01, 2018 at 06:33 PM 0
Share

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

avatar image TreyH mrsev · Feb 01, 2018 at 06:44 PM 1
Share

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);
 }
avatar image mrsev TreyH · Feb 01, 2018 at 07:04 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges