Home > Enterprise >  Checking player is not in scene(Not working)
Checking player is not in scene(Not working)

Time:11-04

I have a game that I want a restart button to appear if you die (The game object destroys itself).

This is what I have. This is the script attached to the button:

public GameObject Player;
public GameObject Button;
bool player;

public void RestartGame(){
    SceneManager.LoadScene("SampleScene");
}

void Start(){
    player = true;
}

void Update(){
    if (!Player){
        player = false;
    }

    if(player == true) {
        Button.SetActive(false);
    }
    if(player == false){
        Button.SetActive(true);
    }
}

And this is the code attached to the object that gets deleted:

void OnTriggerEnter(Collider other)
{
    Destroy(gameObject);
}

When it starts the Button is disabled but when I die nothing appears. I have tried many things like checking if it is == null and all of that stuff but it still doesn't work. My goal is to make the restart button appear once the object is destroyed.

CodePudding user response:

From what I understand you are checking if player get destroyed or not. However you are destroying trigger.

There must be a tag on player object use that. On your trigger object use this code. Pass player and button on inspector.

public GameObject Player;
Public GameObject Button;

void OnTriggerEnter(Collider other) 
{ 
  if (other.gameobject.comparetag("Player"))
  {
    Destroy (Player);
    Button.SetActive(true);
  }
}

For this to work both objects player and trigger must have colliders and at least one of them must have rigidbody , and player tag must be Player

  • Related