Search Unity

Toggle mesh renderer with a Unity event in Animator

Discussion in 'Animation' started by s3509260, Oct 5, 2021.

  1. s3509260

    s3509260

    Joined:
    Oct 4, 2021
    Posts:
    2
    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:

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class toggleMesh : MonoBehaviour
    8. {
    9.     public Renderer rend;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         rend = GetComponent<Renderer>();
    14.         rend.enabled = false;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     public void toggleMeshR()
    19.     {
    20.         rend.enabled = true;
    21.     }
    22. }
    23.  
    Any help with this would be appreciated
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    Your code should work.

    Have you set the animation event to the toggleRenderer() function?
    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:
    Code (CSharp):
    1.     public void toggleMeshR()
    2.     {
    3.         Debug.Log("Function was called.");
    4.  
    5.         rend.enabled = true;
    6.     }
    There's nothing wrong with your code, so the problem likely lies elsewhere. Keep me updated, and good luck!