r/UnityHelp • u/EnderF • 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
View all comments
Show parent comment
1
u/R4nd0m_M3m3r Mar 16 '26
It's different, if I remember correctly this will only work if you're calling the method with the object assigned to its native type variable. If you cast it to base and call, it won't work.