- Home /
 
A question about creating items using scriptable objects
So, I have the item code as follows:
 using UnityEngine;
 
 [CreateAssetMenu]
 public class Item : ScriptableObject{
     public string name = "Dagger";
     public Sprite sprite;
     public int maxQuantity = 9;
     public string description = "An iron dagger";
 
     public enum ItemType {
         Quest, Weapon, Helmet, Armor, Boots, Usable
     }
 
     public ItemType itemType;
 
     public virtual void Use() {
         Debug.Log ("Used item");
     }        
 
     public virtual void OnEquip() {
         Debug.Log("Equiped item" + this.name);
     }
 }
 
               This code is an extended version of Unity's Adventure Game tutorial at Unite, I have created the inventory that is fully functional as I need it, however, I want to create item scripts which are deriving from my Item class and have them function just as any other item. So I want to have, for an example, a public class HealthPotion which derives from Item, and this needs to have an override Use function. How would I go about writing this code? If I just do the health potion like this:
 using UnityEngine
 
 public class HealthPotion : Item {
     public override void Use() {
         Debug.Log("Do something");
     }
 
 }
 
               Then, it probably won't work for reasons I don't know.
Answer by jmgek · Feb 24, 2017 at 08:21 PM
 public class Item : ScriptableObject{
 public virtual void Use()
     { 
          print("Using Base item class");
     }
 }
 
  public class HealthPotion : Item {
      public override void Use() {
          //base.Use() will call the base function
          base.Use();
          Debug.Log("Using Health Potion");
      }
 
              Your answer
 
             Follow this Question
Related Questions
Item database with override functions 0 Answers
[C#]Inventory script help. 3 Answers
Scriptableobject List and Instantiating objects from it 3 Answers