Hey there,
I’m having trouble with a simple transition.
I’m just trying to get a collected coin to transition into an effect animation that I added.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class CoinCollectedAnimation : MonoBehaviour
{
public Animator coinCollectedController;
private void Start()
{
coinCollectedController = GetComponent<Animator>();
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Coin"))
{
coinCollectedController.SetTrigger("collected");
}
}
}
I have a prefab of the Coin with its own animation (moving up & down).
I added the effect animation (animation I want played when the character collides with it) to the Coin controller.
I created the transition to the new animation as well as a trigger parameter and added the behaviour to the transition.
The coin also has a 2d box collider that is set to on trigger.
The coin just does not transition into the new animation.
Any help would be appreciated.
Thanks!
I’d either debug the code, or add some Debug.Log statements, to be sure you’re actually exciting the code, and that the object has the expected tag on it. As long as you’re sure that’s true, how about showing a screenshot of your Animator, showing the trigger and the transition you’re using.
Tried Debug.Log and it doesn’t look like the code is executing and I’m not sure why.
I have attached the Animator and Inspector.
The coin animation plays just fine.
![]()
The animation stuff doesn’t matter if your OnTriggerEnter2D isn’t being called. Things to check for:
- Since you’re using OnTriggerEnter2D, make sure you’re using the 2D version of the colliders. Those methods won’t trigger for normal colliders. You need the 2D version.
- I don’t use 2D physics, but I expect that the collision handling is similar to 3D, where at least one of the objects will need a rigidbody on it for the trigger to fire. You’d have to read up on how collisions work in 2D.
So, get it so that the Debug.Log statements are getting called. Then the animation stuff might just work, assuming you’ve set up the rest properly.
My character does have a Rigidbody and the colliders are 2D.
It works fine for destroying the object (initially thought it was this, but it still doesn’t work when I don’t have the coin destroy on collision) as well as adding score, so I know it’s interacting okay.
I appreciate you taking time to help. If you have any other ideas, let me know.
BTW, your game looks great! Love puzzle games, looking forward to it
It’s possible you don’t have the right tag on the coin object, or your CoinCollectedAnimation isn’t on the script that contains the collider?
Yup, the coin has the coin tag, collider and script