• 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 Desertmoongw · Sep 25, 2019 at 10:45 PM · scriptableobjectinstanceitemsunique

How to create an individual altered ScriptableObject?

I'm currently banging my head against a wall because I can't figure out how to do something that should be fairly simple. Basically, my game has a lot of items in it, and some of those items will be unique and be the only ones do a specific complicated series of tasks, I decided to make my items ScriptableObjects for the ease of implementing them through the inspector but I can't figure out how I can alter the method of a ScriptableObject instance without it altering everything else.

A very simplified example of what I have:

 public class Item : ScriptableObject{
     public string ItemName;
     
     public virtual void Use(){
        //Does generic item whatever that every item does.
     }
 }

.

 public class HealingItem : Item{
     public int AmountToHeal;
     
     public override void Use(){
         base.Use();
        //Heal the player.
     }
 }

and there is one and only one item in the game that has a somewhat complicated usage where it heals you but it also heals slightly over your max health but only for a short time and it makes you invulnerable for a few seconds and a bunch of other stuff.

I could put this in the above HealingItem class and have a toggle to check if it's that one item, but it would be annoying and messy to have a hundred toggles for a hundred unique actions that only happen on unique individual items.

I could make another C# Script that extends HealingItem and has all the complicated stuff and then create an instance of that, but that would also be pretty messy as it means I need to create two files for every unique effect and it would quickly clog up the right click Create menu.

So, how can I do this? Or if I can't, what would be a better solution?

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 WarmedxMints · Sep 25, 2019 at 11:01 PM

I would probably do it a different way round. Have the item class and then add actions to it. That way you can have an object which just heals or one that heals and makes the player invincible and so on. You can have a list of item actions and iterate through them. That way, with a few actions, you can create many combinations.

 public class Item : ScriptableObject
 {
     public List<ItemAction> Actions;
 
     public string ItemName;
 
     public void Use()
     {
         //Does generic item whatever that every item does.
 
         var count = Actions.Count;
 
         for(var i = 0; i < count; i++)
         {
             Actions[i].Use();
         }
     }
 }
 
 public abstract class ItemAction : ScriptableObject
 {
     public abstract void Use();
 }
 
 public class HealPlayer : ItemAction
 {
     public int HealAmount;
 
     public override void Use()
     {
         //Heal Player
     }
 }
 
 public class Invincibility : ItemAction
 {
     public int InvincibilityDuration;
 
     public override void Use()
     {
         //Make Player Invincible
     }
 }
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 Mouton · Sep 26, 2019 at 08:29 AM

You could achieve the same as @WarmedxMints suggest with a unique Item class and UnityEvents (https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html).

Your Item class defines a UnityEvent effects field, then in the inspector you can define the different effects you require.

 public class Item : ScriptableObject
 {
     public string ItemName;
     public UnityEvent effects;
          
     public virtual void Use(){
         if (effects != null)
             effects.Invoke();
     }
 }

You can use a static class ItemEffects where all the applicable effects are stored,

 public static class ItemEffects
 {
     public static Heal()
     {
     }
 
     public static Poison()
     {
     }
 }

If you need to pass arguments to your effects, you can use the generic version of Unity events (UnityEvent) and pass a struct/class.

 public class Item : ScriptableObject
 {
     public string ItemName;
     public UnityEvent<MyParams> effects;
          
     public virtual void Use(MyParams params){
         if (effects != null)
             effects.Invoke(params);
     }
 }
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

119 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

Related Questions

Where is ScriptableObject.CreateInstance stored? 2 Answers

Unique variables for ScriptableObjects? 1 Answer

creating instances 1 Answer

Inventory and scriptable object items - please point me in the right direction 1 Answer

Instantiate a COMPLETELY Unique Instance of an Object 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges