- Home /
Quick Question About Overriding in C#
Hello,
i have two classes, living thing and player, player inherits from living thing. In living thing i have an update loop that checks if the health is
public class LivingThing : MonoBehaviour {
public bool invinsible = false;
public float health = 10;
public void Update () {
if(!invinsible && health<=0)
Kill();
}
public void Kill(){
Destroy(gameObject);
}
}
public class PLayer: LivingThing {
public void Kill(){
gameObject.renderer.enabled = false;
}
As for your LivingThing class you could name it Entity/LivingEntity, that's what $$anonymous$$ecraft does, this is just a suggestion.
Answer by JasonBricco · May 21, 2014 at 03:03 AM
You would define it in the LivingThing class as:
public virtual void Kill()
{
Destroy(gameObject);
}
Then you would define it in Player as something like this:
public override void Kill()
{
gameObject.renderer.enabled = false;
}
This would override the LivingThing's Kill method and it would be called in the child class (Player). But if you also want to call it in the parent class as well, you would use:
base.Kill();
within the Player's Kill method.
Edit:
So to call it in the Player class first and then finish by calling in the LivingThing class:
public override void Kill()
{
gameObject.renderer.enabled = false;
base.Kill();
}
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Overriding an initial value in a subclass? 3 Answers
Question about Overriding 1 Answer