Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 nataliewee5234 · May 14, 2021 at 09:01 AM · gameobjectpanelclosegameover

Unable to close the Game Over's panel after clicked a button

I am unable to close the Game Over's panel after clicked a button. In Resume() I use gameOver.gameObject.SetActive(false); but to no avail. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class HeartFYP : MonoBehaviour
 {
     public GameObject heart1, heart2, heart3, gameOver;
     public static int health;
 
     public Button gameOverReplayButton;
 
     // Start is called before the first frame update
     void Start()
     {
         health = 3;
         heart1.gameObject.SetActive(true);
         heart2.gameObject.SetActive(true);
         heart3.gameObject.SetActive(true);
         gameOver.gameObject.SetActive(false);
 
         lifeBuy.isLifeSold = PlayerPrefs.GetInt("IsLifeSold");
         gameOverReplayButton.interactable = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (health > 3)
             health = 3;
 
         switch (health)
         {
             case 3:
                 heart1.gameObject.SetActive(true);
                 heart2.gameObject.SetActive(true);
                 heart3.gameObject.SetActive(true);
                 break;
 
             case 2:
                 heart1.gameObject.SetActive(true);
                 heart2.gameObject.SetActive(true);
                 heart3.gameObject.SetActive(false);
                 break;
 
             case 1:
                 heart1.gameObject.SetActive(true);
                 heart2.gameObject.SetActive(false);
                 heart3.gameObject.SetActive(false);
                 break;
 
             case 0:
                 heart1.gameObject.SetActive(false);
                 heart2.gameObject.SetActive(false);
                 heart3.gameObject.SetActive(false);
                 //FindObjectOfType<AudioManager>().PlaySound("GameOver");
                 gameOver.gameObject.SetActive(true);
                 Time.timeScale = 0;
                 break;
         }
 
         {
             lifeBuy.isLifeSold = PlayerPrefs.GetInt("IsLifeSold");
 
             if (PlayerPrefs.GetInt("IsLifeSold", lifeBuy.lifeTotalQuantity) == 0)
             {
                 gameOverReplayButton.interactable = false; //disable button
             }
             else
             {
                 gameOverReplayButton.interactable = true; //enable button
             }
         }
     }
     public void yesButton()
     {
         Resume();
         lifeBuy.lifeTotalQuantity = PlayerPrefs.GetInt("IsLifeSold") - 1;
         PlayerPrefs.SetInt("IsLifeSold", lifeBuy.lifeTotalQuantity);
     }
 
     public void Resume()
     {
         gameOver.gameObject.SetActive(false);
         Time.timeScale = 1f;
         heart1.gameObject.SetActive(true);
         heart2.gameObject.SetActive(false);
         heart3.gameObject.SetActive(false);
         Debug.Log("Close Panel...");
     }
 
 }
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 GDGames0302 · May 14, 2021 at 10:37 AM

Hi. Your mistake is the switch function in the Update function. That is executed every frame. When the player dies and the health variable is 0, in the switch function you activate that panel. When you click the Resume button, your panel is deactivated, but is immediately activated by the switch function. You probably have a separate script where you decrease the player's health if something happens. When you decrease the health in that script, set a bool to true. Then, execute this switch function in the Update only if that bool is true. Don't forget to set the bool to false in the switch function. I don't have time to write any code for you, but I just wanted to explain how you can achieve what you want. Of course, there are other ways too. Hope it helps.

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

179 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 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

ui panel disappears after showing for few seconds ?(problem) 1 Answer

What the settings panel in Unity? 1 Answer

GameObject.setActive(true) not working despite children and parents are active 1 Answer

How do I make Game Over text stay enabled for a few seconds before the Application Load function runs? 1 Answer

Detect UI Panel click 2 Answers


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