- Home /
Question by
UnseenCream · Feb 28, 2015 at 11:54 PM ·
parentclassinheritanceattribute
How can I redefine attributes of a parent in the child class?
Hello,
I need to redefine certain attributes (initial variables) for the child class laserTower, which is extended ( a child of) the tower class;
Laser Tower Code:
public class laserTower : tower {
public int attack = 3;
public int variance = 0;
public float range = 2f;
public int attSpeed = 0;
}
Tower Code:
public class tower : MonoBehaviour {
public int attack = 15;
public int variance = 10;
public string type = "poison";
public float range = 1f;
public int attSpeed = 2;
public bool cooldown = false;
}
I am getting an error for reserializing all of the variables:
"The same field name is serialized multiple times in the class or it's parent class. This is not supported: Base(laserTower) attSpeed"
It is not a fatal error, and it still works weirdly enough. But positive its not good practice.
How can I avoid these while still redefining my attributes
Comment
Answer by karljj1 · Mar 01, 2015 at 12:00 AM
Your creating variables with the same name in the child classes. Are you sure you want to do this? If you just want to change the values then do this in the Reset function.
public class laserTower : tower
{
void Reset()
{
attack = 3;
range = 2;
// ...
}
}