N
N
nano_e_t_42020-04-24 17:03:18
.NET
nano_e_t_4, 2020-04-24 17:03:18

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)
    }


if hasInteract is true, then the debug function will still be called in the One object. How to make sure that this function is not called because the flag is set to true

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2020-04-24
@nano_e_t_4

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)
    }

3 - upd .. well, to understand the essence overrideand base
4 - and you can also master the debugger. in VS on F11 go through the steps and understand everything)))

S
sofronov, 2020-04-24
@sofronov

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 question

Ask a Question

731 491 924 answers to any question