Toggle mesh renderer with a Unity event in Animator

Hi guys I’m pretty new to unity and I am trying to make an animation where some of the meshes within a prefab only appear ~halfway through the animation.

I have added an event at the desired time within animator but I cant figure out exactly what code I need to execute to turn the mesh renderer on.

So far I have this:

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

public class toggleMesh : MonoBehaviour
{
    public Renderer rend;
    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();
        rend.enabled = false;
    }

    // Update is called once per frame
    public void toggleMeshR()
    {
        rend.enabled = true;
    }
}

Any help with this would be appreciated

Hey,

Your code should work.

Have you set the animation event to the toggleRenderer() function?
7548697--932980--Event.JPG
(First click on the animation event in the timeline)

If you have that set, you can check if the function is actually running by inserting a Debug.Log() line into your code:

    public void toggleMeshR()
    {
        Debug.Log("Function was called.");

        rend.enabled = true;
    }

There’s nothing wrong with your code, so the problem likely lies elsewhere. Keep me updated, and good luck!