Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved How to make a GameObject Prefab appear…as Animation Event, on an animation frame of character

Discussion in 'Scripting' started by unitedone3D, Sep 11, 2023.

  1. unitedone3D


    Jul 29, 2017
    Hi there! I am trying to get a gameobject prefab to ‘appear’ (be triggered) at a specific frame of an animation of a character. I learned that it is ‘Animation Event’ (on the animation itself) you must use to make ‘animation events’; so I opened the animation (in Animation window), clicked ‘add event’ at the desire frame, and then in ‘game object slot’, I added the desired prefab; but nothing happens, the prefab does not appear in the game at the frame - when the character plays this animation, no gameobject/prefab appears.

    Now, I understand there must be some ‘signal’ to ‘get the prefab’ at that specific frame of the animation. I read that GameObject.SetActive…could be one… tried it does not work.
    Invoke method, SetActive …or GetObject…the empty box fields say Int, Float, String…I’m not sure what is suppose to be the ‘right’ call to get the prefab at that frame (is it a string, bool…you must type there)…as a Animation Event.
    Any help is greatly appreciated and thank you very much in advance.

    PS: If you know, what would be the inverse…to make a gameobject prefab… disappear/turn off - at that frame.
  2. wideeyenow_unity


    Oct 7, 2020
    I played with animation event once, and if I remember correctly the function has to be within the same object the animation is in. And also has to be your own public function, not a Unity method. So make a public function:
    Code (CSharp):
    1. public void HandleMyAnimEvent01()
    2.     {
    3.         gameObject.SetActive(false);
    5.         // or
    7.         GetComponent<Renderer>().enabled = false;
    8.     }
    But as far as the difference between disabling a gameObject, versus just turning off it's renderer, is purely up to you on how you want the situation handled. Does it need to no longer exist, or just be invisible? Use the correct method for the correct case.

    Also is recommended to have the renderer cached in the class associated, so you wouldn't need to get the component, just call it:
    myRenderer.enabled = false;

    But make your own function, and use the anim event to call that, and see if it works. :)
    unitedone3D likes this.
  3. Kurt-Dekker


    Mar 16, 2013