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 Tempest74 · Oct 04, 2017 at 07:59 AM · scripting probleminheritancescriptable object

inheritance from a ScriptableObject

So... I want to inheritance a class from a Scriptableobject but I have some concerns. How it should look? Let's say I make a Item class Inheritance from ScriptableObject. I want to make a weapon class witch inheritance from item, but in the same time i want to inheritance it from ScriptableObject. Can someone help me? How something like that should look? If I remember good you can inheritance from more than one class. And ScriptableObject is a class, right?

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

Answer by Hellium · Oct 04, 2017 at 08:30 AM

First thing, no, you can't inherit from more than one class (in C#).

Then, if you have a class B inheriting from class A, and class C inheriting from class B, then, indirectly, the class C inherits from class A. So your weapon will be a scriptable object thanks to the transitivity property of inheritance.

Multilevel_Inheritance

 // Item.cs
 public abstract class Item : ScriptableObject { /* ... */ }

 // Weapon.cs
 [CreateAssetMenu( fileName = "Weapon", menuName = "Items/Weapon" )]
 public class Weapon:  Item { /* ... */ }
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 Jeshira · Mar 20, 2018 at 01:01 AM

Inheritance means it will have everything from the parent plus stuff of his own and often overridden functions of the parent.

 //Generic class that can't be instantiate.
 public abstract class A: ScriptableObject { 
 
   //Only accessible from children.
   protected int iVar;
 
   public int getVar(){ return iVar;}
 }
 
 //Will also inherit from ScriptableObject.
 public class B: A {
   public B(int _iVar){
    //You can write to inherited protected variable, not private ones.
     iVar = _iVar;
   }
 }
 
 //Can create object and call parent public functions like this.
 B b = new B(10);
 b.getVar(); //Will return 10.

This works to a certain extend. As said before, you can't inherit from two classes. Some way that is normally used is Component-Based Design, but those doesn't get any serialization done by the inspector by default. Component-Based Design is exactly how GameObjects are designed with various components in the inspector.

Attribute.cs

 public enum AttributeType {
   Elemental,
   Category
 }
 
 public class abstract Attribute : scriptableObject {
   protected AttributeType attribute;
   public Attribute { get { return attribute; }}
 
   //Children have to override this.
   public abstract void effect(GameObject _target);
 }

Elemental.cs

 public enum ElementalType {
   Fire,
   Ice
 }

 [CreateAssetMenu( fileName = "Elemental", menuName = "Attributes/Elemental" )]
 public class Elemental : Attribute {
   public ElementType element;
   public Elemental(){
     attribute = AttributeType.Elemental;
   } 
 
   override void effect(GameObject _target){
      //Do something.
   }
 }

Category.cs

 public enum CategoryType {
   Weapon,
   Armor
 }

 [CreateAssetMenu( fileName = "Category", menuName = "Attributes/Category" )]
 public class Category : Attribute {
   public CategoryType category;
   public Category(){
     attribute = AttributeType.Category;
   } 
 
   override void effect(GameObject _target){
      //Do something.
   }
 }

Item.cs

 [CreateAssetMenu( fileName = "Item", menuName = "Items/Item" )]
 public class Item : scriptableObject {
   public List<Attribute> attributes;
   public void Use(GameObject _target){
     foreach(Attribute attribute in attributes)
         attribute.effect(_target);
   }
 }

You'll have to create each ScriptableObjects (Weapon, Armor, Fire, Ice) and drag them in the list. Hence if you want a Fire Sword and a Fire Ax with different Fire damage value, you'll need two different Fire scriptable, but if effect is same for both Fire (set player status to burn), then you only need one.

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.

Follow this Question

Answers Answers and Comments

122 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

Related Questions

Reference to scene objects from scriptable objects 1 Answer

Inheriting Parent Rotation and position 2 Answers

How to store data in script and attach it later? 0 Answers

Scrolling combat text works on enemy. When I duplicate and have two enemies I get a problem. 1 Answer

Inheritance Implementation Weapon add System for the Pro Military: FPS Character? 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