Search Unity

Question Pause Menu Canvas deactivates itself on collision

Discussion in 'Editor & General Support' started by ReptilianAlien, Mar 2, 2021.

  1. ReptilianAlien

    ReptilianAlien

    Joined:
    Mar 21, 2017
    Posts:
    25
    I am not sure what is causing it there is nothing set in my scripts to cause pause menu canvas to set itself to deactive on a collision. It works until something in the seen hits character and it sets itself to deactive. I don't know if this is a bug or not any help would be much appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PauseMenu : MonoBehaviour
    7. {
    8.     public static bool GameIsPaused = false;
    9.  
    10.     public GameObject pauseMenuUi;
    11.     public GameObject healthBar;
    12.    
    13.     void Update()
    14.     {
    15.         if (Input.GetKeyDown(KeyCode.Escape))
    16.         {
    17.             if(GameIsPaused)
    18.             {
    19.                 Resume();
    20.             } else
    21.             {
    22.                 Pause();
    23.             }
    24.         }
    25.     }
    26.  
    27.     public void Resume ()
    28.     {
    29.         healthBar.SetActive(true);
    30.         pauseMenuUi.SetActive(false);
    31.         Time.timeScale = 1f;
    32.         GameIsPaused = false;
    33.         Cursor.lockState = CursorLockMode.Locked;
    34.     }
    35.  
    36.     void Pause ()
    37.     {
    38.         pauseMenuUi.SetActive(true);
    39.         healthBar.SetActive(false);
    40.         Time.timeScale = 0f;
    41.         GameIsPaused = true;
    42.         Cursor.lockState = CursorLockMode.Confined;
    43.     }
    44.  
    45.     public void LoadMenu()
    46.     {
    47.         Time.timeScale = 1f;
    48.         SceneManager.LoadScene("MainMenu");
    49.     }
    50.  
    51.     public void QuitGame()
    52.     {
    53.         Debug.Log("Quiting game");
    54.         Application.Quit();
    55.     }
    56. }
     
  2. ReptilianAlien

    ReptilianAlien

    Joined:
    Mar 21, 2017
    Posts:
    25
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class HealthBar : MonoBehaviour
    6. {
    7.     public Slider slider;
    8.  
    9.     public void SetMaxHealth(int health)
    10.     {
    11.         slider.maxValue = health;
    12.         slider.value = health;
    13.     }
    14.  
    15.     public void SetHealth(int health)
    16.     {
    17.         slider.value = health;
    18.     }
    19. }
    20.  
     
  3. ReptilianAlien

    ReptilianAlien

    Joined:
    Mar 21, 2017
    Posts:
    25
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerHealth : MonoBehaviour
    7. {
    8.     public int maxHealth = 100;
    9.     public int currentHealth;
    10.     public int damage = 25;
    11.     public int healAmount = 50;
    12.  
    13.     public HealthBar healthBar;
    14.  
    15.     public GameObject heart;
    16.  
    17.     Animator animator;
    18.     bool isDead = false;
    19.  
    20.     void Start()
    21.     {
    22.         currentHealth = maxHealth;
    23.         healthBar.SetMaxHealth(maxHealth);
    24.         animator = GetComponent<Animator>();
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         DeathCheck();
    30.     }
    31.  
    32.     void TakeDamage(int damage)
    33.     {
    34.         currentHealth -= damage;
    35.         if (currentHealth < 0) currentHealth = 0;
    36.         healthBar.SetHealth(currentHealth);
    37.     }
    38.  
    39.     void Die()
    40.     {
    41.         if (!isDead)
    42.         {
    43.             animator.SetBool("isDead", true);
    44.             FindObjectOfType<GameManager>().EndGame();
    45.             isDead = true;
    46.          }        
    47.     }
    48.  
    49.     void DeathCheck()
    50.     {
    51.         if (currentHealth <= 0 && !isDead)
    52.         {
    53.             Die();
    54.             isDead = true;
    55.         }
    56.     }
    57.  
    58.  
    59.     void Heal(int healAmount)
    60.     {
    61.         currentHealth += healAmount;
    62.         if (currentHealth > maxHealth) currentHealth = maxHealth;
    63.         healthBar.SetHealth(currentHealth);
    64.     }
    65.  
    66.     void OnCollisionEnter (Collision collisionInfo)
    67.     {
    68.         if (collisionInfo.collider.tag == "Enemy")
    69.         {
    70.             TakeDamage(damage);
    71.             Debug.Log("oucheeeee");
    72.         }
    73.  
    74.  
    75.         if (collisionInfo.collider.tag == "Heart")
    76.         {
    77.             Heal(healAmount);
    78.             Destroy(heart);
    79.             Debug.Log("Restored Health");
    80.         }
    81.  
    82.        // if (collisionInfo.collider.tag == "GameOver")
    83.         //{
    84.             //if (currentHealth <= 50)
    85.            // {
    86.                // GetComponent<PlayerMovement>().enabled = false;
    87.              
    88.             //}
    89.             //else
    90.             //{
    91.                 //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    92.            // }
    93.        // }
    94.     }
    95.  
    96.  
    97.     void OnTriggerEnter(Collider collider)
    98.     {
    99.         if (collider.CompareTag("Enemy"))
    100.         {
    101.             TakeDamage(damage);
    102.             Debug.Log("oucheeeee");
    103.             animator.SetBool("isDamage", true);
    104.         }
    105.  
    106.  
    107.         if (collider.CompareTag("Heart"))
    108.         {
    109.             Heal(healAmount);
    110.             Destroy(heart);
    111.             Debug.Log("Restored Health");
    112.         }
    113.     }
    114.  
    115.     void OnTriggerExit(Collider collider)
    116.     {
    117.         if (collider.CompareTag("Enemy"))
    118.         {
    119.             animator.SetBool("isDamage", false);
    120.         }
    121.     }
    122.  
    123. }
    124.  
     
  4. ReptilianAlien

    ReptilianAlien

    Joined:
    Mar 21, 2017
    Posts:
    25
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class MainMenu : MonoBehaviour
    7. {
    8.     public void PlayGame ()
    9.     {
    10.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    11.     }
    12.  
    13.     public void QuitGame ()
    14.     {
    15.         Debug.Log("Game Has Been Exited");
    16.         Application.Quit();
    17.     }
    18. }
    19.  
     
  5. ReptilianAlien

    ReptilianAlien

    Joined:
    Mar 21, 2017
    Posts:
    25
    Sorry i figured it out I just needed to sleep. It was attached to the healthbar it had it disabled on hit for some reason.