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
1
Question by unity_Lvt62BoYah2LuA · Mar 07, 2018 at 11:54 AM · gameobjectscriptableobjectdataturn-basedcombat

use ScriptableObject as player data (rpg combat)

So , I'm creating a simple turn-based combat (think angry birds epic RPG) , and I stumble on ScriptableObject and use it. basically I created an EnemyBase and PlayerBase ScriptableObject and create an asset for player and enemy from baseSO. (for starter it's just HP and sprite)


how do you pass the ScriptableObject value to the gameObject? because AFAIK SO is not present in the scene itself, do we still need a presistent gameObject to store the value?. let's say in a transition to battleScene, the battleScene has 6 blank gameObject (3 enemies and 3 players). how do we get all the value from SO (lets say player1,2,3 / enemy1,2,3) to the respective gameObject?

can we modify the SO value for saving player progress? (let's say the HP got upgraded after a few battle) , or again is it better to have presistent gameObject that only recieve baseValue from SO but later on we only modify the gameObject?


ScriptableObject looks very light compared to prefab so I wanted to maximize the use of SO instead of single prefab for every player / enemy

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by itchyOwl · Mar 08, 2018 at 03:55 PM

On creating scriptable objects:

You don't need that ScriptableObjectUtility anymore, because CreateAssetMenuAttribute should work similarly. If you really want an implementation that does not require adding that attribute, you need to use reflection to get all the scriptable object types in the project. Throw me a msg, if you need that. The code is a bit lengthy, so I'm not posting it here.


To be clear, I'm referring scriptable objects as instances stored in assets. Scriptable objects can be instantiated like other Unity Objects without storing them to assets, but I don't have any experience on using them like that.


On using scriptable object instances as assets:

After storing the scriptable object instance as asset, you use it like any other asset. They are not stored in the scene, but in the assets folder (like models, textures, script files etc). You don't need to copy the data from the scriptable object into a script, you can simply use a reference to it.


You have at least three options to make a reference to it: A) drag the reference in the editor B) load the asset from Resources (in runtime) C) use an asset bundle and get it from there (in runtime).


You can also use a scriptable object to store references to other assets. This way you only have to load the scriptable object and get all the other references via it.


Just remember that scriptable objects are not for saving the game data, because they cannot be modified outside the editor! Use binary/text serialization, json, xml, playerprefs or some other solution for that.

Comment
Add comment · 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
1

Answer by Harinezumi · Mar 07, 2018 at 12:31 PM

Using ScriptableObject is a great way of storing data for levels, creatures or anything. That said, you need a way to save instances as assets, which is done with an editor script. Fortunately, the Unity Wiki has a generic implementation for it (I tried, it works perfectly). EDIT: as itchyOwl writes, this isn't necessary anymore, the CreateAssetMenuAttribute can be used for this purpose.
So after you create your ScriptableObject assets you can declare a field in your creature logic and assign the asset to it (possibly with [SerializeField] if you want to assign it in the Editor).
Also, check out this video that explains some of the amazing uses of ScriptableObjects!

Comment
Add comment · Show 4 · 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 unity_Lvt62BoYah2LuA · Mar 07, 2018 at 04:48 PM 0
Share

so how do you use the SO value? let's say I have a goblin and bat asset , do I assign them as the EnemyBase asset or just get the value from EnemyBase asset? (and implement them in correspond field like float for hp , string for name etc.). also there is a possibility to either get goblin/bat value (let's say random encounter). do I create a list of all the enemy asset and use integer as index check for enemyAsset List and spawn the index? or is there any better way to implement these?


also the createScriptableObjectAsset you share is basically [CreateAsset$$anonymous$$enu] attributes, right?

avatar image Harinezumi unity_Lvt62BoYah2LuA · Mar 08, 2018 at 08:26 AM 0
Share

The way I do it (but I'm still experimenting, so this might not be the best approach) is that I have a Creature script (I assume for you that's EnemyBase) that has a field for a CreatureAttributes ScriptableObject. Then I create different CreatureAttributes assets, and assign them to the Creature, either at run-time, when a game object with Creature on it is created, or I can already have it assigned in a prefab.
So I think for random encounters I would do just what you said, put different CreatureAttributes assets into a List, and randomly select one and assign it to the Creature.

I hope this makes sense!

avatar image unity_Lvt62BoYah2LuA Harinezumi · Mar 08, 2018 at 02:28 PM 0
Share

okay that actually makes sense xD

so basically Creature script is a container for GameObject that needs a CreatureAttributes .asset value. quick question tho , how do you assign the assets during play session? I usually assign the asset via inspector , but for GameObject that has wild card value (not always assigned with myAsset1) , I can't seem to find a way to do it via script or any special method from ScriptableObject

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

113 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

Related Questions

Instantiating a Game Object and using Scriptable Objects 1 Answer

Displaying text on a GameObject 1 Answer

ScriptableObject Inheritance 1 Answer

Get a parent GameObject from a class on a script attached to a prefab,Getting a parent Gameobject from a component on a script attached to a prefab 1 Answer

Data with reference to scene objects: Monobehaviour or ScriptableObject? 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