Game Over Using Events - Unity 2D C#

Hello everyone, this is my first post here so I am sorry if there are any errors. I’ve been trying to implement a Game Over screen w/ a designated winner for a Pong game. I am fairly new to coding, so there might be a lot of errors and some stuff that doesn’t make sense in my code. I have looked up tutorials and have tried to find answers on here already, but after multiple trial and errors I’ve just decided to ask on my own.


Anyway, we are supposed to use an Event to trigger a Game Over screen when one of the players reaches 5 points. It is the game Pong, so points are scored when a player gets a ball past the other person’s paddle. I have a int maxScore = 5; and My HUD is supposed to invoke a “PlayerWonEvent” (the event has a UnityEvent ScreenSide parameter attached to it). This is the code that I have for it where the HUD keeps track of score:

`void AddPoints(ScreenSide side, int points)
    {
        // add points and change text
        if (side == ScreenSide.Left)
        {
            leftScore += points;

            if(leftScore >= maxScore)
            {
                playerWon.Invoke(side);
            }
        }
        else
        {
            rightScore += points;

            if(rightScore >= maxScore)
            {
                playerWon.Invoke(side);
            }
        }
        scoreText.text = leftScore + ScoreSeparator + rightScore;
    }`

There is a GameplayManager script that listens for the event, sets the winner, and instantiates a GameOver prefab from a Resources folder:

public class GameplayManager : MonoBehaviour
{
    PlayerWonEvent playerWon = new PlayerWonEvent();
    GameOverMessage gameOver; 

    /// <summary>
    /// Update is called once per frame
    /// </summary>
	void Update()
    { 
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            MenuManager.GoToMenu(MenuName.Pause);
        }
    }

    /// <summary>
    /// Ends the game and displays the winner message
    /// </summary>
    /// <param name="winner">winning side</param>
    public void EndGame(ScreenSide winner)
    {
        gameOver.SetWinner(winner);
        MenuManager.GoToMenu(MenuName.GameOver);
    }

    public void AddPlayerWonListener(UnityAction<ScreenSide> listener)
    {
        playerWon.AddListener(listener);
    }
}

The code shows that I tried added the GameOver prefab as a Menu so the GameplayManager goes to it when the event is trigged, but I have yet to see anything happen. The game just keeps on going no matter how many points are scored. I have also tried to simply instantiate it as an object when the event is trigged, but that hasn’t worked either. I assume that either the HUD script is incorrect and isn’t keeping track of when a player reaches 5 points, or I have set up the Invoker/Listener pieces incorrectly.


This is the code for my GameOverMessage script for clarity sake and also if something is incorrect in there:

public class GameOverMessage : MonoBehaviour
{
    [SerializeField]
    Text playerOne;
    [SerializeField]
    Text playerTwo;


    void Start()
    {
        Time.timeScale = 0;
    }
    /// <summary>
    /// Shows the game over message with the given winner
    /// </summary>
    /// <param name="winner">winning side</param>
    public void SetWinner(ScreenSide winner)
    {
        if(winner == ScreenSide.Left)
        {
            playerOne.text = "Game over, player one wins!";
        }
        else
        {
            playerTwo.text = "Game over, player two wins!";
        }
    }

    /// <summary>
    /// Hides the game over message and returns to the main menu
    /// </summary>
    public void HandleQuitButtonClick()
    {
        Time.timeScale = 1;
        Destroy(gameObject);
        MenuManager.GoToMenu(MenuName.Main);
    }

This project is for a class, so I would prefer answers that help me figure out what’s wrong rather than just giving me the answer. Or if you do just give me the answer, a brief explanation would be welcomed. I try not to ask for help very often, because I know it can negatively impact my learning experience, but I am really at a loss here. Thank you.

I managed to figure this out on my own, the question was in moderation for a while and I had a due date. Thank you.