Events not working in build, work in editor play mode

Hello,
I have a few animations, that have animation events. its an external package that provides a script attached to the game object with the animator, and the function is called OnCustomEvent.

    /// <summary>
    /// Animation events. If you want to get animation callback, use it.
    /// For example, if you want to know exact hit moment for attack animation, use custom event 'Hit' that is fired in most attack animations.
    /// </summary>
    public class AnimationEvents : MonoBehaviour
    {
        /// <summary>
        /// Subscribe it to get animation callback.
        /// </summary>
        public event Action<string> OnCustomEvent = s => { };

I subscribe to the OnCustomEvent with the following code:

        foreach(Collider2D enemy in hitEnemies)
        {
            Debug.Log("We hit " + enemy.name);
           
            /*var damage = Random.Range(15, 30);
            var enemyScript = enemy.GetComponent<Enemy>();
            if (enemyScript)
            {
                TakeDamage(player, enemyScript, damage);
            }*/
            enemyHits.Add(enemy);

        }
       
        animationController.OnCustomEvent += OnHit;
    }
   
    public void OnHit(string text)
    {
        GameObject.Find("DebugLog").GetComponent<TextMeshProUGUI>().text = text;
        foreach (var enemy in enemyHits)
        {
            var damage = Random.Range(15, 30);
            var enemyScript = enemy.GetComponent<Enemy>();
            if (enemyScript)
            {
                TakeDamage(player, enemyScript, damage);
            }
        }
       
        enemyHits = new List<Collider2D>();
    }

The problem is, the code works when I am in playmode, but when i build the game, the OnHit subscriber never seems to get called.

Is any of that failing? Check the runtime logs to see. Or move it to the end of the function.

Also, I understand this is just debug code, but remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

Its working in Development, but not when the game is built

Then focus 100% of your attention on the built version.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like

it seems the subscriber does not get called

I found the error. After investigating the player.log file, I found an object not found reference exception in runtime, that did not show up in the editor. should be a bug.

Fix:
Change

    private List<Collider2D> enemyHits;

To

    private List<Collider2D> enemyHits = new List<Collider2D>();
1 Like

I hate it when someone is right and I was on the wrong path. Had the same problem, thought about events too and in the end it were several vars where GetComponent() was too slow or some event fired on an deleted object.
Good to find a solution not good, that this stuff seems to happen only in release, which makes debugging harder -_-

I’m going to move this to the Scripting forum. It’s not related to 2D.