Timer after Collision to make an Object's MeshRenderer Disappear

Hi all,

I start off my game with all object’s mesh renderer’s turned off. When the player collides with the mesh renderer, I have it to be turned on. How would I start a “timer,” so that after a certain duration after colliding, the objects mesh renderer would turn off and become invisible again until the next collision? I started reading things into Couroutines and IEnumerators, but am still really confused.

Also just wanted to note that I am very new and have little unity experience so anything would be greatly appreciated!

public class Test : MonoBehaviour
{
MeshRenderer meshRenderer;
private void Start()
{
meshRenderer = GetComponent();
meshRenderer.enabled = false;
}

    private void OnCollisionEnter(Collision collision)
    {
        meshRenderer.enabled = true;
        StartCoroutine(DisableRenderer());
    }

    IEnumerator DisableRenderer()
    {
        yield return new WaitForSeconds(3f);
        meshRenderer.enabled = false;
    }
}

Hi, this could be a simple example-script for you.

meshRenderer.enabled = false; on void Start make your object invisible.

If OnCollisionEnter, make object visible and Start Corutine that make it invisible again after 3 seconds.

Look into this example and I believe you can write your own, proper code for your project :slight_smile: