- Home /
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.
I Know I Am Late And I Know This Isn't What Your Looking For, But The Script Is Good
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
}
}
Answer by byrne · Jan 13, 2015 at 04:22 AM
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:
Alpha - set to 1 for full visibility of this element and its children. 0 for full invis.
Interactable - set to true if it has buttons that require mouse interaction events.
Block raycasts - set to false if you want the user to be able to click through all nodes.
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!
Answer by DwaynePritchett · Jan 11, 2015 at 04:20 AM
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.
Thanks for the help but it doesn't Works, only a part of it I think.
The error in console is this.
NullReferenceException: Object reference not set to an instance of an object GameOverScript.OnEnable () (at Assets/Scripts/GameOverScript.cs:123) UnityEngine.GameObject:AddComponent() NaveJugador:OnDestroy() (at Assets/Scripts/NaveJugador.cs:244)
And the script modified by me is this.
public Button buttonRestartLevel;
public Button buttonBack;
void OnEnable()
{
buttonRestartLevel.gameObject.SetActive(true);
buttonBack.gameObject.SetActive(true);
}
void OnDisable()
{
buttonRestartLevel.gameObject.SetActive(false);
buttonBack.gameObject.SetActive(false);
}
$$anonymous$$aybe it doesn't Works because gameObject.SetActive doesn't Works for UI canvas?
I've searched on scripting API but there's nothing for disable or enable canvas renderer
http://docs.unity3d.com/ScriptReference/CanvasRenderer.html http://docs.unity3d.com/ScriptReference/Canvas.html
Answer by DavidRoad · Feb 19 at 01:25 AM
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
}
}