• 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
Question by efergan · Dec 01, 2017 at 11:09 AM · inheritancepropertiesserializevariable-definition

Setting Child Class Vars while not getting: "The same field name is serialized multiple times in the class or its parent class."

Hey Everyone,

I've think I've searched the archive properly, but couldn't find a suitable answer. So let me try and ask here...

Ok, so basically here's my setup and problem.

I have two classes (well, I have more, but two should illustrate things). The parent class 'Weapon' and the child class 'Launcher'.

Understandably, I want to define and hold the shared core weapon variables in that parent class. But... I want each child class to be able to set these variables, and more importantly have them visible in editor to be tweaked per player, but each come with defaults.

So, without the confusion of a parent class I'd simply add [SerializeField] to a number of private variables, include defaults in the declaration, and get on with it.

But when extending the parent class I use the 'new' keyword to refer to these variables and set defaults, and include the [SerializeField] in the child class.

Have a look at the code (well, a single example variable from the code):

  public class Weapon : MonoBehaviour {
     
         // protected but visible in editor via 'new' in children
         protected float damage;
     
     }
     
     public class Launcher : Weapon {
     
         // using variables specific to child class made visible in editor
         [SerializeField]
         new protected float damage = 5f;
     
     }


Works perfectly... when I add the child component I can see the correct variables fields, can edit them, and they refer the parent variables.

So WHY! am I receiving "The same field name is serialized multiple times in the class or its parent class. This is not supported: Base(Launcher) damage". If I take the child reference out if obviously fixes it, but there's no sign of it in the editor, so the parent class is not trying to serialize it. Is it because extending monobehaviour serializes everything? How do I make the editor be quiet?

Thanks. Dan

Comment

People who like this

0 Show 1
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 agray427 · Jan 16, 2018 at 08:42 PM 0
Share

I don't understand why are you setting it up like this in the first place?

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

73 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

Related Questions

Trying to make a simple quest system, but i stuck how to do 0 Answers

I need to make two bullet controllers (friendly and enemy) Should I inherit? 1 Answer

When or where do I set a prefab script subclass property? 2 Answers

C# Parent-SubClass set inherited properties help 1 Answer

An OS design issue: File types associated with their appropriate programs 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