Button Show and Hide

I am trying to Hide and Show a button. On start the button is hidden, I want to show the button once the player dies, here is my code.

Now when I change the gameObject.SetActive(false); on start to true, it shows the button and it keeps showing when dead because of the code in the update, but when its false on the start, it doesnt show it at all, even after death.

What am I missing here?

public class PlayButton : MonoBehaviour
{

public PlayerMovment playerScript;
// Use this for initialization
void Start()
{

gameObject.SetActive(false);

}

// Update is called once per frame
void Update()
{

if (playerScript.dead)
{
gameObject.SetActive(true);

}

}
}

You are setting the button to show up but not resetting it to hidden when the player comes back. I assume your reset is not a level/scene reset. From the way you described though it seems your playerScript.dead isn’t that reliable to begin with. Assuming it is reliable enough however, this is one short way you could set the button active or visibility in the update.

gameObject.SetActive(playerScript.dead);

This is weird!! When I use gameObject.SetActive(playerScript.dead); in the update, the button doesnt show up at all, whether its true or false in the start :confused:

The button should show up after the player dies, it is connected to a trigger in the canvas to restart the game once the button is clicked.

Like i said before, your player.dead does not seem that reliable to begin with.

How do you suggest I do it? I am still a beginner. If you can suggest a tutorial or a site I would be thankful.

I don’t know how your playerScript.dead works. Can’t suggest any course of action if i don;t know the problem.

When I use this code it works though, it does disable and enable the button but its always visible.

GetComponent<Button>().enabled = false;
GetComponent<Button>().enabled = true;

You are simply running the 2 lines in order, so enabled will always be true since the 2nd line always overrides the 1st line.

No, I mean I used it like this.

So I set up the gameObject.SetActive(true); to true in the setup just to give you an idea,

This is what happens, when I first start the button is showing but its not clickable, after i die the button becomes clickable, so how come the GetComponent().enabled = false/true work and the gameObject.SetActive(true/false); doesnt work :confused:

    public PlayerMovement playerScript5;
    // Use this for initialization
    void Start()
    {

        gameObject.SetActive(true);
        GetComponent<Button>().enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (playerScript5.dead)
{
    gameObject.SetActive(true);

    GetComponent<Button>().enabled = true;

}

Just remembered, from what I understand about Unity’s UI, it is better to enable and disable the Canvas the button is on.

This is how my code works, the PlayerMovement playerScript5; creates an object of the class PlayerMovement and in the update it says when the object from that class is .dead true, then do what ever is in the if statement.

Now in the PlayerMovement class this is how i set up the dead.

public class PlayerMovement : MonoBehaviour {
public bool noDeath = false;
public bool dead = false;

void FixedUpdate () {

        if (dead)
            return;
}

void OnCollisionEnter2D(Collision2D collision)
    {
       
        if (noDeath)
            return;
        dead = true;
        animator.SetTrigger("Death");
    }
}

The noDeath is just for testing purposes to enable god mode.

Okay I will try that.

Unfortunately the same result :confused:

Okay it finally worked when I used the GetComponent().enabled = false/true;

Thanks a lot and I appreciate your help :slight_smile:

Update doesn’t run on inactive GameObjects. So once your object was inactivated, it can’t reactivite itsself.

Moving the OP code to a different GameObject would fix the problem.