Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by TheGalacticShaman · Jan 27, 2019 at 10:09 AM · scripting beginnerscriptableobjectsavinginstantiationrandomization

Creating new scriptable objects at runtime

Hey guys, so I am working on a fighting game where you manage a team. I would like the player to be able to pick there team members from a list of randomly generated fighters.

So far, I have some scriptable objects that hold different stats (eg name, health, strength) that are assigned to the fighters. However, these need to be set in the editor. I would love to know if there is a way to create new scriptable objects at run time, and generate their stats randomly. This scriptable object would then need to be saved to be accessed between scenes or when the game is later opened.

It should be possible for an infinite amount of characters to be created.

Any help would be greatly appreciated, or an alternative to using scriptable objects would be great !

For clarification, a similar system would be in pokemon. You stumble upon randomly generated pokemon, each one belonging to a specific "race" but that individual having its own distinct and random stats. Once caught, it is saved.

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
3
Best Answer

Answer by Hellium · Jan 27, 2019 at 10:23 AM

You can create Scriptable Objects at runtime by calling CreateInstance


But, keep in mind that these scriptable objects won't be saved automatically in your project (not even on the disk) like the ones you use in the editor. You will have to save and load them by yourself. To do so, you can use a custom serializer or the JsonUtility of Unity + System.IO.File.WriteAllText

Comment
Add comment · Show 9 · 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 TheGalacticShaman · Jan 27, 2019 at 10:38 AM 0
Share

Hey, thanks for your answer ! Im not familiar with the create instance function, do you think you could elaborate a little bit more or provide a very quick/simple example ? I would really appreciate it.

As for the saving/loading, thanks for referring me to some solutions I have found some documentation about them and I think I can look into those a bit more myself.

avatar image BastianUrbach TheGalacticShaman · Jan 27, 2019 at 10:43 AM 1
Share
 var obj = (YourClass)ScriptableObject.CreateInstance(typeof(YourClass));
avatar image Bunny83 BastianUrbach · Jan 27, 2019 at 01:17 PM 2
Share

Note that we have a generic version of the CreateInstance method for a long time already. So you usually do

 var obj = ScriptableObject.CreateInstance<YourClass>();

or if you like to specify the variable types explicitly:

 YourClass obj = ScriptableObject.CreateInstance<YourClass>();


Finally note that ScriptableObject is a UnityEngine.Object derived type. So you can simply use Instantiate to create a clone of an existing one. So the clone will be a completely independent / new object but with the same values as the source from which it was cloned:

 YourClass newObj = Instantiate(obj);
Show more comments
Show more comments

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

110 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

Related Questions

Can you use a ScriptableObject to save game state? 6 Answers

Text Adventure Change rooms and backgrounds 0 Answers

is class variable in a ScriptableObject automaticly Instantiated? 1 Answer

Scriptable Object not saving changes to a list. 0 Answers

Saving changes to the asset files for Prefab and Scriptable objects. 1 Answer


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