(SOLVED) WaitForSeconds OnTriggerEnter

I’m trying to Enter a trigger call a function which sets a bool to true then wait for a second or two and then set the bool to false and also Debug.Log to tell me it worked. The code I currently have doesn’t give me any errors, but it is only setting the bool to true and destroying the gameObject from there it doesn’t seem to do anything. If you can help solve my issue it would be greatly appreciated!

My code is this currently:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoubleNuggets : MonoBehaviour
{

    private GameObject doubler;

    private void Start()
    {
        doubler = GameObject.FindWithTag("coinmanager");

    }

    IEnumerator OnTriggerEnter(Collider collision)
    {
            doubler.GetComponent<CoinsManager>().shouldDouble = true;
            Destroy(gameObject);
            yield return new WaitForSeconds(1);
            doubler.GetComponent<CoinsManager>().shouldDouble = false;
            Debug.Log("Worked");
        }
        }

SInce you are destroying the gameObject, the script stops running. Nothing after the destroy will run because the object no longer exist to keep running.

I figured that out right before you replied thank you though Brathnann! lol.

Another question though how do I get it to destroy the gameobject and still be able to access the script? Is there a way to access the sprite renderer and just turn it off and then destroy gameobject after I set it false?

Nevermind I figured that out also! I used gameObject.GetComponent().enabled = false; and it set it to disable the sprite, but keep the gameObject until after the wait time. All good here now.

Bro… I gotta write it, i was looking around for problem and you solve it… :slight_smile: thank u very much!