Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by PandawanFr · Jan 06, 2017 at 02:52 AM · c#inspectorinheritancefieldinherited-members

Make a field in inspector hold inheriting classes

Hello,

I was wondering if it was possible for a field in the Inspector to change between different classes that extend the given field type.

What I mean is, I have a Weapon class, I also have Gun and Melee, which both extend Weapon. Gun and Melee have their own specific variables but also use some of Weapon's variables.

What I want is for some other script, to have a public variable of type Weapon, and I want to be able to be able to use either a Gun or a Melee inside the field in the Inspector. (So that I am able to edit the variables for that weapon in the inspector no matter what the weapon "type" is).

Here's a quick example

 public class Weapon{
     public string weaponName;
 }
 
 public class Melee : Weapon{
     public float range;
 }
 
 public class Gun : Weapon{
    public float ammoCapacity;
 }

 public class Attack : MonoBehaviour {
    //Can I (inside the Inspector), use a Gun or Melee instead of being forced to a basic Weapon
    public Weapon weapon; 
 }

Thanks!

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

Answer by UnityCoach · Jan 06, 2017 at 01:40 PM

You could make your weapon class a ScriptableObject. Then you would have to create weapons as assets and drag and drop them :

 [CreateAssetMenu (fileName = "New Generic Weapon.asset", menuName = "Generic Weapon")]
 public class Weapon : ScriptableObject
 {
     public string weaponName;
 }
 
 [CreateAssetMenu (fileName = "New Melee.asset", menuName = "Melee")]
 public class Melee : Weapon
 {
     public float range;
 }
 
 [CreateAssetMenu (fileName = "New Gun.asset", menuName = "Gun")]
 public class Gun : Weapon
 {
     public float ammoCapacity;
 }

The only limitation, is that ScriptableObject are only references, so changing one changes them all. Which can also be handy.

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 PandawanFr · Jan 06, 2017 at 04:17 PM 0
Share

The only limitation, is that ScriptableObject are only references, so changing one changes them all. Which can also be handy.

By that, do you mean, that I have to create a new script/class for each separate weapon that I want to make?

avatar image PandawanFr PandawanFr · Jan 06, 2017 at 04:41 PM 0
Share

Ok, I found a great tutorial for this! https://www.youtube.com/watch?v=ItZbTYO0$$anonymous$$nw Thanks a lot!

avatar image UnityCoach PandawanFr · Jan 06, 2017 at 05:30 PM 0
Share

Not a new script/class, but a new asset. Though, using the implementation suggested by @Adam-$$anonymous$$echtley solves this.

avatar image
0

Answer by Adam-Mechtley · Jan 06, 2017 at 07:40 AM

What you're asking about here is polymorphic serialization, which is not supported. I have answered similar questions e.g., here, discussing possible workarounds.

Comment
Add comment · Show 2 · 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 UnityCoach · Jan 06, 2017 at 01:44 PM 0
Share

Er, sorry, didn't read your other post fully :)

avatar image PandawanFr · Jan 06, 2017 at 04:42 PM 0
Share

Scriptable Objects was the way to go! Thanks for the help! I ended up using this video to make my Scriptable Objects https://www.youtube.com/watch?v=ItZbTYO0$$anonymous$$nw

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

268 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

An OS design issue: File types associated with their appropriate programs 1 Answer

HideInInspector with inherited variables 1 Answer

Child of Text class not showing added public variable in insepctor 2 Answers

Hide headers and variables in inspector from parent classes 1 Answer

Parent Class variables in Child Class's Inspector (C#) 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