Why overriding?

Why are we overriding OnDrawerUnLocked when the overridded code can also be entered directly under OnDrawerUnlocked function? Both functions are being executed anyway so why separate them?

private void OnDrawerUnlocked(SelectEnterEventArgs arg0)
{
isLocked = false;
if(keyIndicatorLight != null)
{
keyIndicatorLight.SetActive(false);
}
Debug.Log("****DRAWER UNLOCKED");
}

protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
if (!isLocked)
{
transform.SetParent(parentTransform);
isGrabbed = true;
}
else
{
ChangeLayerMask(defaultLayer);
}
}

The why is because the class that you inherit from offers important functionality and on its own, setting the transform and bool does nothing really. The base.OnSelectEntered calls the original class code which is capable of the interaction specified.

Then why cannot both the code of DrawerUnlocked and its overrided function be put together under DrawerUnlocked function?

Because what you’re overriding is an event handler. It fires automatically when you perform a specific action.

okay thank you

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms