How can child class trigger parent class function/void/event?
I have theese two example scripts:
Child class:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour
{
public void Die()
{
Destroy(gameObject);
}
}
Parent class:
using UnityEngine;
using System.Collections;
public class Player_Health : Health
{
public void ShowDeadScreen()
{
FindObjectOfType<DeadScreen>().Show();
}
}
And now I want to child class trigger "ShowDeadScreen()" in parent class, how?
Please help, thanks! :)
Answer by DiegoSLTS · Sep 08, 2015 at 05:15 PM
A derived (child) class inherits all public methods from the base (parent) class. Just write the name of the method you want to execute.
You can make it an explicit call of the inherited method by using the "base" keyword, but it won't make a differente unless you've overrided the method: https://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx
Anyway, I think you are confused. In your example, "Health" is the base (parent) class and Player_Health is the derived (child) class. ShowDeadScreen is a method of the derived class only, the base class don't have it and you can't call that method on it.
Also, an object of type Player_Health is also of type Health, but it's just one object, you can't call a method "on the parent" class, you call a method on the object itself, and the method is either defined on that class or inherited from the base class.
What are you trying to do exactly?
In Health I need to execute parent Player_Health ShowDeadScreen() void in Die() void...
public void Die()
{
Player_Health.ShowDeadScreen(); //I know it don't work like this but here I want execute parents ShowDeadScreen()
Destroy(gameObject);
}
I guess you can cast "this" to Player_Health and then call the method. Something like:
public void Die() { Player_Health ph = (Player_Health)this; ph.ShowDeadScreen(); Destroy(gameObject); }
Anyway, that's a really REALLY bad approach, it's wrong in so many levels. A base class shouldn't call methods from the derived classes, it gos against the benefits of using polymorfism and inheritance.
Why not just make "Die" virtual, and override it in the derived class adding what you want? If Player_Health is derived from Health and Health is derived from $$anonymous$$onoBehaviour then Player_Health is a $$anonymous$$onoBehaviour and can be added as a component.
Change your scripts to:
using UnityEngine;
using System.Collections;
public class Health : $$anonymous$$onoBehaviour
{
public virtual void Die()
{
Destroy(gameObject);
}
}
And:
using UnityEngine;
using System.Collections;
public class Player_Health : Health
{
public override void Die()
{
ShowDeadScreen();
base.Die();
}
public void ShowDeadScreen()
{
FindObjectOfType<DeadScreen>().Show();
}
}
And then add ONLY Player_Health to your player, then get the Player_Health component and call the Die method on it. It will show the dead screen then call de Die method implementation of the Health class.
Your answer

Follow this Question
Related Questions
How can I start to script an asset? 1 Answer
Can you develop on a consumer edition of an HTC Vive? 1 Answer
I need some scripting help. 0 Answers
Problem with UI Text 0 Answers
Json on cross plataform 0 Answers