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 Mort432 · Apr 14, 2018 at 01:16 PM · inspectordesign-patternspolymorphism

Polymorphic weapon system

Just coming back to using Unity after a long period of time in .NET web development.

I'm prototyping a little top-down shooter, and I'd like to be able to switch out behaviours in the inspector with polymorphism.

My current example is something like this:

  1. The player has a Combat component, which handles input and has a public Weapon variable. Weapon is an interface with a "Fire" function.

  2. I have an implementation of Weapon called "Assault Rifle" which has particular behaviours when Fire is called.

The ideal here is that underneath the Combat component in the inspector, I could simply drag and drop any script that implements Weapon.

I feel like I'm misunderstanding some key Unity principles here. Any help would be really appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Apr 14, 2018 at 01:52 PM

Using an interface does work when you use GetComponent. However references to interfaces can't be serialized by Unity. If you want to be able to assign a behaviour by drag&drop you have to use a baseclass with a virtual / abstract method instead.

Comment
Add comment · Show 3 · 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 Mort432 · Apr 14, 2018 at 02:01 PM 0
Share

Thanks for the reply!

I've managed to get this far. The issue, however, is that I can only drag and drop scripts that exist within the scene.

I'd like to be able to drag, for example, my "AssaultRifle" script from my project scripts folder, and into the Weapon property on my player.

EDIT: Do I need to just create a lot of empty game objects with my weapon scripts on them, and use such prefabs?

avatar image Bunny83 Mort432 · Apr 14, 2018 at 02:21 PM 0
Share

Well you can't store a reference to a "class type" in a variable. (Well technically System.Type would work, but this can not be serialized ^^). If your weapon implementations don't require to be a $$anonymous$$onoBehaviour and to be attached to a gameobject you can derive your base class from ScriptableObject. ScriptableObjects can be stored as assets in the project and referenced by other serialized objects.


Also keep in $$anonymous$$d that you can attach multiple weapon derived $$anonymous$$onoBehaviours scripts to the same gameobject. Though dragging the components to variables involves using two inspectors if the weapon scripts are not on the same gameobject as your referencing script.


To drag individual scripts onto variables you have to drag the script header from the inspector like in this example:

avatar image Mort432 Bunny83 · Apr 14, 2018 at 02:34 PM 0
Share

Thanks, you've been a great help!

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

79 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

Related Questions

Assigning child script to parent field in Inspector 1 Answer

Inspector - editing array of polymorphic objects? 2 Answers

Show DefaultInspector on child classes in list 0 Answers

Draw Inspector For Most Derived Type Of Collection Elements 3 Answers

Serializable class with access to its Monobehaviour host 2 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