user3708761
user3708761

Reputation: 275

Making a button appear after player death - Unity3D 4.6 GUI C#

How would I make it so once the player dies a button appears?

I already have the restart level coded, and I have the button on screen that utilizes that code. How do I make it so the button isn't showing and isn't functional until the player is dead?

In response to below.

public GameObject RESTART_BUTTON;
bool isDead = false;
void Update()
{
    if (isDead == true)
    {
        RESTART_BUTTON.gameObject.SetActive(true);
        Debug.Log("Do show game object");
    }
}

void Start()
{
    if (isDead == false)
    {
        RESTART_BUTTON.gameObject.SetActive(false);
        Debug.Log("Do Not show game object");
    }
}

void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log(collision.gameObject.tag);
    if (collision.gameObject.tag == "Death")
    {
        isDead = true;
        Debug.Log("isDead_true");
    }
}

Here is my console output https://i.sstatic.net/gByew.png

public void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log(collision.gameObject.tag);
    if (collision.gameObject.tag == "Death")
    {
        //Destroy(gameObject);
        isDead = true;
    } // end if including tag collision
} // End OnCollisionEnter

IEnumerator isDeath()
{
    if (isDead == true)
    {
        _animator.Play(Animator.StringToHash("Jump"));
        ;
        yield return new WaitForSeconds(2);
        Destroy(gameObject); //this will wait 5 seconds
    } // end if including boolean isDead
}

Upvotes: 1

Views: 2707

Answers (1)

Archit
Archit

Reputation: 84

public GameObject YourButton;

keep the button inactive while the player is alive. Once he is dead, execute the following code.

YourButton.gameObject.setActive(true);

This would activate the button on the screen.

Add this code in void Update() and change the code in void start() and collision to:

 // // Update is called once per frame
 void Update () {
 if(isDead == true){
    RESTART_BUTTON.gameObject.SetActive(true);
    }
}

void Start () {
    if(isDead == false){
    RESTART_BUTTON.gameObject.SetActive(false);
    }
 }

public void OnCollisionEnter2D(Collision2D collision){
Debug.Log(collision.gameObject.tag);
if(collision.gameObject.tag == "Death"){
    isDead = true;
} 
}

That should do the needful.

Upvotes: 1

Related Questions