Search Unity

Scripting a menu [Noob Problems]

Discussion in 'Scripting' started by qiqette, Feb 8, 2016.

  1. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Well, I have a button "menu" on the UI and i want press it to show a canvas. I have this code:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class menuScript : MonoBehaviour {
    6.  
    7.     public GameObject PauseUI;
    8.     public GameObject menu;
    9.     private bool paused = false;
    10.  
    11.     void Start () {
    12.         PauseUI.SetActive(false);
    13.     }
    14.  
    15.     void Update () {
    16.         if (Input.GetButtonDown("menu")) {
    17.             paused = ! paused;
    18.         }
    19.         if (paused)
    20.         {
    21.             PauseUI.SetActive(true);
    22.             Time.timeScale = 0;
    23.         }
    24.         if (!paused)
    25.         {
    26.             PauseUI.SetActive(false);
    27.             Time.timeScale = 1;
    28.         }
    29.     }
    30. }
    31.  
    When I asociated the "menu" button with my public gameobject menu, but when i press it canvas still hiden
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
  4. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Sooo the code will be:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class menuScript : MonoBehaviour {
    5.  
    6.     public GameObject PauseUI;
    7.     private bool paused = false;
    8.     void Start () {
    9.         PauseUI.SetActive(false);
    10.     }
    11.  
    12.   public void onClick()
    13.     {
    14.         paused = !paused;
    15.         if (paused)
    16.         {
    17.             PauseUI.SetActive(true);
    18.             Time.timeScale = 0;
    19.         }
    20.         if (!paused)
    21.         {
    22.             PauseUI.SetActive(false);
    23.             Time.timeScale = 1;
    24.         }
    25.     }
    26. }