- Home /
Cannot select Animation Event
I've copied an animation in order to make it R/W, added the component script below to the GameObject, then created an Animation Event. However, I cannot select my function (MeshState) from the list. It does not appear on the list. The list is empty.
// MeshHider.cs
using UnityEngine;
using System.Collections;
public class MeshHider : MonoBehaviour {
public Renderer meshSelection;
public bool meshHidden;
public void MeshState(bool meshHidden){
if( meshHidden == true)
{
meshSelection.renderer.enabled = false;
}
else
{
meshSelection.renderer.enabled = true;
}
}
}
Answer by Max_Bol · Aug 20, 2017 at 07:32 AM
I would like to add a tip/hint/possible solution to those who might find themselves in this kind of situation which is resolved quite easily when you know what's happening.
The Event key in the Animation windows in Unity is used by TWO sub-system of Unity's animator system at once. There are what I call a "direct" and an "indirect" sub-system.
Let's explain what is both :
1. The Direct It is Instance based and is only accessible if you select the animation's gameobject through the Hierarchy window. This method requires that you place a script with the desired function on the animated gameobject itself. It's a bit similar to the UI's button component as in which you can have some controls over who's targeted by the event as long as it's done through the script's function. This is the one displayed in the screenshot above.
2. The Indirect This one is based on what scripts are currently awaken in the scene. You got to select the animation through the Project window. This is the kind of event menu displayed in the documentation. It's using a radio-based call which mean that all listeners active in the scene that has that function will activate when it's the event is called if it exist. (TR. It means that any scripts awakened (started) that has the function's name in it will activate the function with the given parameters if available.)
The best kind of example I could give for the 2 are the following :
The direct is useful for specific event that are only happening to 1 gameobject at a time. For example, if you want each step of your character to generate some kind of dust or if you want a melee attack to only count at a specific frame, this is the kind of method you could use.
The Indirect is useful for scene-based events. For example, you could have something like a nuke explosion and have an even in it that send the call "AllDies" where all the NPCs on the map has a script including that kind of function. It could also be related to event that affect AIs such as a call that update the Pathfinder's script of all the NPCs. (Such as if a gate is closed, no NPC try to cross it.)
One thing is required in BOTH way and that's that the function does exist "at least" on any scripts in the actual animated gameobject. The Direct method require a specific script to be attached selected while the Indirect method allow you to use whatever script you want as long as the script is attached to the same Gameobject as the one with the animator. The indirect method is great if you plan on instantiating stuff on the fly (like adding scripts mid-game) like a customization system.
Really deep answer with great and detailed examples. $$anonymous$$ust be included into documentation. Thank You, Sir.
I want to let people know I've read through the documentation, watched videos on Animation Events and they STILL missed this crucial point this guy made and it's still relevant in 2021. Big thanks to everyone for this thread.
Answer by Ixtabay · Jun 28, 2014 at 02:31 PM
The answer was simple, after lots of head scratching. You cannot use boolean! The below works in case anyone else stumbles onto this: using UnityEngine; using System.Collections;
public class MeshHider : MonoBehaviour {
public Renderer meshSelection;
public int meshState;
public void MeshState(int meshState){
if( meshState == 1)
{
meshSelection.enabled = false;
}
else
{
meshSelection.enabled = true;
}
}
}
Yeah, i encountered this problem a while ago, and i was not able to find any kind of answer about it, i had to arrive to the same conclusion by myself too, but why would you set 1 as true? i think it shoudl be 0
Answer by text23d · Feb 27, 2016 at 03:57 PM
Just now this happened to me. Then I remembered that I have attached the script with those functions for AnimationEvents to a prefab. After deleting a script from a prefab, this works again! ;-) (And adding a script to a prefab again, made the list of functions to become empty)
Your answer
Follow this Question
Related Questions
Can you use renderer.enabled on array members ? 3 Answers
Unity 2019.4.9f1 - Toggle Show/Hide Gameobject with one keystroke. 1 Answer
No renderer.enabled=false on grouped GameObject 2 Answers
How to make a prefab active and non-active using script? 1 Answer
I am trying to create a togglable game menu using the ESC key 1 Answer