Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ecaeris · Nov 16, 2020 at 11:05 PM · menuevent triggeringgameover

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ecaeris · Nov 18, 2020 at 08:36 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get back to the main menu when dead? 1 Answer

Is SetActive doesn't working or the button dont work? [Solved] 1 Answer

Menu communication failed 1 Answer

I'm trying to make a list driven menu for items, skills that meet a interactable interface like the one in Persona 3 Portable, can someone help? 0 Answers

How do i add an extra menu to my main menu (I'm a Beginner programmer!) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges