- Home /
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
I don't understand why are you setting it up like this in the first place?