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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to make a button that disappears when clicked

Discussion in 'Getting Started' started by xinyixiao1212, Aug 14, 2018.

  1. xinyixiao1212

    xinyixiao1212

    Joined:
    Aug 12, 2018
    Posts:
    3
    I am new to Unity and I'm trying to make a button that says begin go to the next screen when clicked. It does go to the next screen, but the button remains on the screen. So I'm trying to make the button go away. This is my code currently:

    public void BeginClick()
    {
    Instantiate(Day1[0], new Vector3(0, 0, 0), Quaternion.identity);
    //make the button disappear
    }

    That method is on a ButtonManager C# file and it is linked to the button click. I have tried beginbutton.SetActive(false), but it just changes what is saved in the prefabs, and doesn't affect the Hierarchy at all and on the screen it doesn't do anything. I've also tried Destroy(beginbutton) to no avail. How would I fix this?
     
    Last edited: Aug 14, 2018
    DerrickMoore likes this.
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Welcome! So there's a number of different ways you could do this "properly", but any way that works is fine when you're starting out, I say.

    Since you indicated that calling SetActive on your beginButton property affects a prefab, I'm assuming that your button is being instantiated at runtime using that prefab assignment, yes? If that's the case, you'll want to save a reference to the button when you instantiate it. Then use that to call the SetActive method when you want to hide it. Ex:
    Code (CSharp):
    1. public GameObject pfbBeginButton;   // This is your prefab assignment
    2. private GameObject instBeginButton; // Reference to instantiated prefab
    3.  
    4. void Awake() {
    5.     // Or whenever you instantiate the button
    6.     instBeginButton = (GameObject) Instantiate(pfbBeginButton, Vector3.zero, Quaternion.identity);
    7. }
    8.  
    9. public void BeginClick() {
    10.     Instantiate(Day1[0], new Vector3(0, 0, 0), Quaternion.identity);    // Consider saving a reference to this if you need to hide it later
    11.     instBeginButton.SetActive(false);   // Hides the button!
    12. }
    If you're going to be doing a lot of hiding and showing, you'll want to get more clever with saving references. It feels sloppy to have 100 variables for prefab assignments and 100 for instance references, right? Using a combination of ScriptableObjects and Lists (or even Dictionaries!) could solve that problem for you, when you get there.

    Give that a shot and let us know if it works out. If not, post the rest of the code in your manager so we can see what's what.

    Good luck!
     
    UnanchoredStudios and MD_Reptile like this.
  3. xinyixiao1212

    xinyixiao1212

    Joined:
    Aug 12, 2018
    Posts:
    3
    Yes, thanks for your reply! I tried your method by saving a reference then hiding it, but it doesn't seem to work. Here's my code so far.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour {
    6.  
    7.     public GameObject Titlescreen;
    8.     public GameObject pfbBeginButton;
    9.     private GameObject instBeginButton;
    10.     public GameObject[] Day1;
    11.  
    12.  
    13.    
    14.    
    15.     void Awake () {
    16.         Instantiate(Titlescreen, Vector3.zero, Quaternion.identity);
    17.         instBeginButton = (GameObject)Instantiate(pfbBeginButton, Vector3.zero, Quaternion.identity);
    18.     }
    19.  
    20.     public void BeginClick()
    21.     {
    22.         Instantiate(Day1[0], Vector3.zero, Quaternion.identity);
    23.         instBeginButton.SetActive(false);
    24.     }
    25.  
    26.  
    27.  
    28.  
    29. }
    30.  
    Day1[] is an array of all the screens that are shown on day 1 of the game and Day1[0] is the first screen to be displayed. Thanks for your help!
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Put a breakpoint at line 23 and run the game. Does that point get hit for sure? Is your menu or screen or whatnot being instantiated?
     
  5. xinyixiao1212

    xinyixiao1212

    Joined:
    Aug 12, 2018
    Posts:
    3
    I did some debugging and it seems that on Awake, instBeginButton starts out as null, and when
    instBeginButton = (GameObject)Instantiate(pfbBeginButton, Vector3.zero, Quaternion.identity);
    it becomes a clone of pfbBeginButton, but then when BeginClick is executed, the value of instBeginButton is null again.

    And like I said, everything else is working like it should be. The button makes the title screen go to the next day screen, but the button isn't disappearing.
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Are you changing scenes or reinstantiating the GameManager? Doing this will clear out any properties that were assigned, which would fit with what you're seeing happen.

    It's really tough to say without seeing the whole setup.