Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 leonardoraele · Feb 02, 2014 at 03:07 PM · inspectorattributepropertyabstract

Can I refer to a custom abstract class as a MonoBehaviour property?

Hi, I'm new to Unity and C#; and this is my first question. Here is my problem:

In my MonoBehaviour C# script, I have a reference(public attribute) to an abstract class I created in a second C# script. See the example:

Abstract class:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public abstract class CustomBehaviour {
 
     public abstract void Activate(GameObject gameObject);
 
 }

My MonoBehaviour:

 using UnityEngine;
 using System.Collections;
 
 public class OnCollisionEvent : MonoBehaviour {
 
     public CustomBehaviour behaviour;
 
     public void OnCollisionEnter(Collision collision)
     {
         if (behaviour != null)
         {
             behaviour.Activate(this.gameObject);
         }
     }
 
 }

The problem is, in the Inspector of my GameObject, when I put a script component with OnCollisionEvent, the Inspector does not show the behaviour property to setup.

I understand that this occurs because CustomBehaviour class is abstract and I can't create any instance, and if there's no instances, there's are no attributes.

What I wanted is that I could select my concrete instance of behaviour property(a derived class of CustomBehaviour) in the inspector, like this:

My concrete CustomBehaviour:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class DieBehaviour : CustomBehaviour {
 
     override public void Activate(GameObject gameObject)
     {
         Object.Destroy (gameObject);
     }
 
 }

So that Unity would instantiate a concrete CustomBehaviour of my choise for behaviour attribute of OnCollisionEvent script.

How can I do that? And if not possible, what is the best approach in Unity to have dynamic custom behaviours like this? (Similar to Gof's Strategy Pattern)

Comment
Add comment · Show 3
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 Jamora · Feb 02, 2014 at 05:58 PM 0
Share

I would imagine this could be achieved with an enum which is set from the inspector. A custom strategy would then be instantiated based on that enum value, e.g. in Awake...

Alternatively, you could make your custom behaviors ScriptableObjects, then create an asset for each object and assign those in the inspector.

Neither is a good, robust solution, but I'm hoping someone with more time can come up with an answer.

avatar image gfoot · Feb 02, 2014 at 08:54 PM 0
Share

$$anonymous$$y gut feeling is that you should be able to get what you want if you write a custom property drawer for your abstract type and make it chain through a virtual method to let the derived behaviour instances offer whatever fields they want. For null it would need to allow the user to provide an instance, for example by offering an enum selector to chose a type to construct.

I would seriously consider going down the ScriptableObject route though. If you want multiple objects to share the same behaviour instances - including settings - then this is the way to go, and you'll be able to drag and drop them into the inspector just like any other first class asset.

avatar image leonardoraele · Feb 04, 2014 at 03:13 AM 0
Share

The problem with ScriptableObjects is that if my OnCollisionEvent have a CustomBehaviour (and assu$$anonymous$$g that CustomBehaviour is a subclass of ScriptableObject), I won't be able to drag DieBehaviour assets to a OnCollisionEvent component in the inspector, only CustomBehaviour assets.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Feb 04, 2014 at 03:47 AM

The serialization system can't serialize your custom class in your case. Inheritance for custom-serializable classes doesn't work at all. However, is there a reason you don't want to use MonoBehaviour as baseclass for CustomBehaviour? It would be bound to the instance, can be serialized properly. If you want to have one instance for all objects that share the same CustomBehaviour then i would also suggest ScriptableObject as baseclass.

Since DieBehaviour is derived from CustomBehaviour it can always be assigned to a CustomBehaviour field, that's one of the major points of inheritance.

Just to clarify: custom classes with the serializable attribute aren't really serialized as own instance. Only the fields of the instance get serialized along with the MonoBehaviour which contains the reference to the instance.

In most cases i would suggest MonoBehaviour as baseclass since it's way easier to use than ScriptableObject. You can also use a prefab as instance holder if you only want one instance. The prefab can be assigned to multiple references if you like.

Comment
Add comment · Show 1 · 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 leonardoraele · Feb 04, 2014 at 10:01 PM 0
Share

Reusability

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

21 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

Related Questions

List of Inherited abstract Objects assigned via Inspector 1 Answer

Inspector attribute for showing a field with a custom name? Possible? 1 Answer

[Range] attribute saves to undo-history too often. 1 Answer

Abstract Inspector-Manipulable Objects that aren't GameObjects? 2 Answers

Add AudioSource-Like Range to Script 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