UI 4.6 canvas button, how to make it invisible on gameplay and how to make it visible when player is dead?

Hello community.

How can I call UI 4.6 canvas button when GameObject is Destroyed?

Before on UI legacy I called like this example on the script “player”

public void OnDestroy()
	{
			// Game Over
        if (Application.isLoadingLevel)
        {
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            transform.parent.gameObject.AddComponent<GameOverScript>();
        }
		 
	}

I want to make those buttons invisible and do it visible when player is dead. But I don’t know how I can do it.

I’ll appreciate any help.

Regards.

Hi, I am currently doing a lot of work on unity uGUI. If you would just like to make something invisible but still retain references and control over your objects, use the component ‘CanvasGroup’ on any of your ‘Canvas’ objects or its children UI nodes.

There are 4 options you can use on this component at this time:

  1. Alpha - set to 1 for full visibility of this element and its children. 0 for full invis.
  2. Interactable - set to true if it has buttons that require mouse interaction events.
  3. Block raycasts - set to false if you want the user to be able to click through all nodes.
  4. Ignore Parent Groups - this doesn’t really work at the time of this post, set it to false and ignore this option.

Try to set these variables in code in your Start() or Awake() functions or another function, because they have quirky behaviors like resetting their flags after some random action. It has been already reported.

Hope this helps!

public class GameOverScript : MonoBehaviour
public GameObject canvasButton;
void OnEnable(){
canvasButton.SetActive(true);
}

void OnDisable(){
     canvasButton.SetActive(false);
}

Something like this should work. Make a public GameObject and drag and drop your UI Button in inspector. Then turn the GameObject on and off with SetActive.

This is how I would do it at least.

EDIT* WARNING* This code won’t work as is. It’s pseudo code to help get the point across.

I Know This Isn’t What Your Looking For, But Try It

using System;
using UnityEngine;
using System.Collections;

public class PlayerDeath : MonoBehaviour {

	void Start()
	{
		//playerdeath
	}

	void OnTriggerEnter()
	{
		Application.LoadLevel("DeathScene");               //will load death scene
		Debug.Log("Player Has Been Killed");               //on console it will say Player Has Been Killed
	} 
}