Answer the question
In order to leave comments, you need to log in
How to trigger an interrupt in a child class?
Hello everyone
Who knows how can I make stop the execution of the child function in the parent?
clarify with an example
class Number: MonoBehavior
protected bool hasInteract
public virtual void OnTriggerStay(Collider other)
{
if (hasInteract)
return;
}
class One : Number
public override void OnTriggerStay(Collider other)
{
base.OnTriggerStay(other);
Debug.Log(other.gameobject.name)
}
Answer the question
In order to leave comments, you need to log in
1 - base.OnTriggerStay(other);
returns something from itself
2 - something like this
class One : Number
public override void OnTriggerStay(Collider other)
{
if (hasInteract)
return;
Debug.Log(other.gameobject.name)
}
override
and base
What for? Just in this form, the task contradicts the very meaning of inheritance.
The child is always programmed after the parent in order to get the new behavior known at the time of writing.
In this case, if you want the function to not be called with a certain flag value, then you need to check the flag value before calling. Fortunately, it is protected, not private and is available to the child.
If further behavior should depend on more complex conditions that are not available in children, then you need to reconsider the architecture and either start returning the result by the OnTriggerStay method, or add a field to the Collider parameter type that indicates the need for further processing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question