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.
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
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.
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
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;
}
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.