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

How to Hide and Disable Buttons in Unity 4.6 (Solved)

Discussion in 'UGUI & TextMesh Pro' started by maxrio, Apr 6, 2015.

  1. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    Greetings. I need to complete a level, two buttons appear. One to end the game and another to restart the level. What is the procedure if possible in javascript? Thank You.
     
  2. Mol

    Mol

    Joined:
    Mar 11, 2015
    Posts:
    15
    Hi,
    I'm new to unity but i will try to help.
    I see two ways of doing this :
    1) Use SetActive()
    2) Create the buttons at RunTime when the lvl is completed

    The first one is easier :

    1)Create and set up your 2 buttons and inactivate them (top left in the inspector)
    2)Link the buttons to a script like this one :

    It's c# i don't use Javascript, sorry.
    But the key is to inactivate the buttons and activate them with SetActive.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UIManager : MonoBehaviour {
    6.  
    7. public Button Restart;
    8. public Button End;
    9. // Call this function when the lvl is completed
    10. public void ShowButtons()
    11. {
    12. Restart.SetActive(true);
    13. End.SetActive(true);
    14. }
    Hope this work for you.
     
    maxrio likes this.
  3. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    Thank you.
    I will test and post the results.
     
  4. maxrio

    maxrio

    Joined:
    Jan 8, 2015
    Posts:
    25
    The hint now works well. Just did some modifications because of the way that you passed me was not working. I'm putting the javascript code. I do not know C # well. Thank you for everything.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. import UnityEngine.UI;
    4.  
    5. var restart : Button;
    6. var end : Button;
    7.  
    8. function Start () {
    9.  
    10. }
    11.  
    12. function Update ()
    13. {
    14.         restart.gameObject.SetActive(true);
    15.         end.gameObject.SetActive(true);
    16. }