I have a problem, I have 3 classes, one is a base class from which two other inherit, the base class has a method with a counter, if the time runs out it should destroy gameObject, but other classes should have this method but in this conditional statement you could was to add anything I just want to add an extra command to if from another class.
If you don't understand, feel free to ask, because I'm not good at explaining.
public class A : MonoBehaviour
{
public virtual void DestroyByTime()
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0)
{
/*
This statement should only be added in class B
|
v
gameSession.GameOver();
*/
Destroy(gameObject);
}
}
private void Update()
{
DestroyByTime();
}
}
public class B : A
{
public override void DestroyByTime()
{
/*
I want the base class if to be saved, but in if statement time runs out you lose
(gameSession.GameOver())
*/
}
}
CodePudding user response:
You can create an OnTimeOut method an override only in derived class:
public class A : MonoBehaviour
{
public virtual void DestroyByTime()
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0)
{
OnTimeOut();
Destroy(gameObject);
}
}
protected virtual void OnTimeOut()
{
// Do nothing here
}
private void Update()
{
DestroyByTime();
}
}
public class B : A
{
protected override void OnTimeOut()
{
gameSession.GameOver();
}
}