- Home /
Can't Activate/Deactivate Object!
I can't seem to activate the Death Message in my game when my player dies. I want the message to display for 5 seconds and then disappear, unfortunately I am not getting any error messages so I don't know what it wrong. Here is my code:
using UnityEngine;
using System.Collections;
public class FallDeath : MonoBehaviour {
private int scene;
public GameObject DeathMessage;
void OnTriggerEnter(Collider other) {
scene = Application.loadedLevel;
Debug.Log ("Triggered");
if (other.gameObject.name == "FPSController") {
DeathMessage.SetActive(true);
Debug.Log("Activated");
System.Threading.Thread.Sleep(5000);
DeathMessage.SetActive(false);
Debug.Log("Deactivated");
Application.LoadLevel(scene);
}
}
}
Answer by abhi_360 · May 22, 2015 at 10:43 AM
void OnTriggerEnter(Collider other)
{
scene = Application.loadedLevel;
Debug.Log ("Triggered");
if (other.gameObject.name == "FPSController") {
StartCoroutine(Wait());
}
}
//Add Coroutine
IEnumerator Wait()
{
DeathMessage.SetActive(true);
Debug.Log("Activated");
yield return new WaitForSeconds(5);
DeathMessage.SetActive(false);
Debug.Log("Deactivated");
Application.LoadLevel(scene);
}
should work try it
System threads take parameters in ms so 5000ms = 5s
It did not work, no death message and yet the level still reloads
ok the code tested and robust so if its not working check if anything else (else meaning script) has a handle to your deathmessage gameobject.
It may be some other script is resetting it to stay disabled
Check :
-if the gameobject is assigned in the inspector
-if its renderer is enabled
-if its color alpha is not 0
The code above should work.
Your answer
Follow this Question
Related Questions
Cannot deactivate / activate gameobject, even in editor! 6 Answers
How to access the properties of any created Game Object from another script Unity C # 2D 1 Answer
Deactivate an object - and all scripts in that object deactivated? 1 Answer
Camera switch between child back to main camera issue 2 Answers