r/UnityHelp • • Mar 16 '26

PROGRAMMING New method doesn't hide old method

I have a Button class with an OnHit() method:

public void OnHit()
{
    if (!isPressed) 
    {
        buttonSound.Play();
        button.Play("Press Button");
        isPressed = true;

        if (iActivatable != null)
        {
            iActivatable.GetComponent<IActivatable>().Activate();
        }
    }
}

method works great. Now I'm making a button doing extra stuff so I made a new class called PuzzleButton. I want it to do the same as a normal button so I'm inheriting from Button, but I want to do more stuff than normal in OnHit(), so I hid it:

public class PuzzleButton : Button
{
    public int buttonID;                // number of this button
    public PuzzleManager puzzleManager; // reference to the puzzle manager

    public new void OnHit()
    { 
        base.OnHit();
        Debug.Log("Puzzle Button hit");
        puzzleManager.PressButton(buttonID);
    }
}

Unfortunately, I never reach the debug line so I must have made a mistake. For now I have a temporary workaround, but I'd like to be able to make this code work. Can someone help me?

2 Upvotes

4 comments sorted by