Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Toggle Issues

Discussion in 'UGUI & TextMesh Pro' started by anarionragnor1986, Jun 21, 2015.

  1. anarionragnor1986

    anarionragnor1986

    Joined:
    May 23, 2014
    Posts:
    8
    Hello, been struggling with this issue all day. I've searched, and searched but nothing. I am trying to get a component of my ui to be toggled when I hit a button (kind of like when you hit a button to access the main menu).

    Here is my script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class HideFight : MonoBehaviour {
    6.  
    7.     public float alpha;
    8.     public CanvasGroup canvasGroup;
    9.  
    10.     void Update()
    11.     {
    12.        
    13.         if (Input.GetKeyDown("escape"))
    14.         {
    15.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().alpha = 1;
    16.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().interactable = true;
    17.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().blocksRaycasts = true;
    18.         }
    19.    
    20. }
    21.     }
    My problem is, I can enable the interface, but I don't know how to disable the interface without using another button. I have tried many things, but I have had no luck. I want to be able to set alpha to 0 and interactable/blocksRaycasts to false, while using the same button.
     
  2. anarionragnor1986

    anarionragnor1986

    Joined:
    May 23, 2014
    Posts:
    8
  3. anarionragnor1986

    anarionragnor1986

    Joined:
    May 23, 2014
    Posts:
    8
    I have seriously tried everything I can think of, no matter what I search I get no answers, someone please help. I can get it to work fine with two buttons, but for some asinine reason it does not want me to use a button.. any button more than once.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class HideFight : MonoBehaviour {
    6.  
    7.     public float alpha;
    8.     public CanvasGroup canvasGroup;
    9.     public bool collision;
    10.  
    11.     void Update()
    12.     {
    13.        
    14.         if (Input.GetKeyDown("escape"))
    15.         {
    16.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().alpha = 1;
    17.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().interactable = true;
    18.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().blocksRaycasts = true;
    19.             }
    20.         else if (alpha >= 1 ||Input.GetKeyDown("escape"))
    21.         {
    22.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().alpha = 0;
    23.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().interactable = false;
    24.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().blocksRaycasts = false;
    25.            
    26.            
    27.            
    28.         }
    29.  
    30.         }
    31.  
    32.  
    33.    
    34. }
    35.  
     
  4. anarionragnor1986

    anarionragnor1986

    Joined:
    May 23, 2014
    Posts:
    8
    Well after two days of bashing my head against the wall, I solved my problem by myself. I will post it here for those who need help with the same issue that I had, because no one here wants to help a guy in need.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class FightMenu : MonoBehaviour {
    6.  
    7.     private bool isKeyPressed = false;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.  
    18.         if (Input.GetKeyDown ("escape") && !isKeyPressed)
    19.         {
    20.             isKeyPressed = true;
    21.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().alpha = 1;
    22.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().interactable = true;
    23.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().blocksRaycasts = true;
    24.            
    25.         }
    26.         else if (Input.GetKeyDown("escape") && isKeyPressed)
    27.         {
    28.             isKeyPressed = false;
    29.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().alpha = 0;
    30.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().interactable = false;
    31.             GameObject.Find ("Fight Canvas").GetComponent <CanvasGroup>().blocksRaycasts = false;
    32.            
    33.            
    34.         }
    35.  
    36.  
    37.     }
    38. }
    39.  
     
    SAOTA likes this.
  5. JackSparrow1

    JackSparrow1

    Joined:
    Jun 21, 2015
    Posts:
    16
    Im having a similar problem with getting a button scrips that is part of a panel that is part of a canvas switched on with my main js code. Ive tried implementing your code but cant even get it to compile. js/C issue.
    best i could do was to get all non canvas elements switched off on my event (value reached:switch off) but i cant target it to the specific UI element i need.
     
  6. SAOTA

    SAOTA

    Joined:
    Feb 9, 2015
    Posts:
    220
    I completely disable my canvas gameobjects. Haven't had any issues.

    gameobject.setactive(false); I think half of anything I've written in c# has this.
     
  7. OldManAnimal

    OldManAnimal

    Joined:
    Jul 10, 2014
    Posts:
    45
    You'll probably want to store that canvasGroup in a reference.

    Doing a find and getcomponent 3 times each time you want to show/hide this is pretty wasteful.

    So in Start or Awake do the find and getcomponent and store it in a variable then do the same toggling of the groups settings to hide/show it.

    Code (CSharp):
    1.  
    2. private CanvasGroup myCanvas;
    3.  
    4. void Awake()
    5. {
    6.     myCanvas = GameObject.Find("Fight Canvas").GetComponent<CanvasGroup>();
    7. }
    8.  
    9. void Update()
    10. {
    11.     if (Input.GetKeyDown("escape"))
    12.     {
    13.         if (myCanvas.interactable)
    14.         {
    15.             myCanvas.alpha = 0;
    16.             myCanvas.interactable = false;
    17.             myCanvas.blocksRaycasts = false;
    18.         }
    19.         else
    20.         {
    21.             myCanvas.alpha = 1;
    22.             myCanvas.interactable = true;
    23.             myCanvas.blocksRaycasts = true;
    24.         }
    25.     }
    26. }
    27.  
     
    harleyjd and SAOTA like this.
  8. lermy3d

    lermy3d

    Joined:
    Mar 16, 2014
    Posts:
    101
    You guys might probably want to check out this link, hope it helps!
    Have a nice day!
    Lermy