Hello everyone. I am new to Unity and I need a hand with a script for a jumpscare. I have attempted it and put all the triggers etc. Here is my attempt that I try but all I get are errors
public GameObject jumpscareObject;
void Start () {
jumpscareObject.SetActive(false);
}
void OnTriggerEnter (Collider player) {
if(player.tag == "Player")
{
jumpscareObject.SetActive(true);
yield return new WaitForSeconds(1.5f);
Destroy(jumpscareObject);
Destroy(gameObject);
}
}
Like @ShadyProductions said, you need to use a coroutine if you want to use the delay. I would reorganize what you have above like this:
public GameObject jumpscareObject;
void Start()
{
jumpscareObject.SetActive(false);
}
void OnTriggerEnter(Collider player)
{
if (player.tag == "Player")
{
StartCoroutine(DoTheSpook());
}
}
IEnumerator DoTheSpook()
{
jumpscareObject.SetActive(true);
yield return new WaitForSeconds(1.5f);
Destroy(jumpscareObject);
Destroy(gameObject);
}
Hope that helps!
IEnumerator OnTriggerEnter (Collider player)
{
if(player.tag == “Player”)
{
jumpscareObject.SetActive(true);
yield return new WaitForSeconds(1.5f);
Destroy(jumpscareObject);
Destroy(gameObject);
}
}
OnTriggerEnter can be used as a coroutine just give it the proper return type.