I am working on an air hockey game in Unity where I’m trying to trigger a UI animation whenever a goal is scored. However, the animation is not playing as expected when the goal event is triggered, and I’m struggling to figure out why. Here’s a detailed breakdown of the issue and what I’ve tried so far.
Project Setup:
- Goal Animation: I have a GoalText GameObject, which is an image in the UI, and it has an Animator attached. The animator is responsible for playing a goal animation when a goal is scored.
- Gameplay: When the puck enters the goal, the Goal script should call the GameplayUIManager script to trigger the goal animation.
- Scripts Involved:
- GameplayUIManager.cs: Manages the UI elements and triggers the goal animation by calling goalText.SetTrigger(“GoalScored”).
- Goal.cs: Detects when the puck enters the goal and calls the GameplayUIManager to play the goal animation.
- Puck.cs: Manages the puck’s physics and respawn logic.
Problems Encountered:
-
Animation Not Playing: The goal animation does not play when the puck enters the goal. The animation plays when I manually trigger it in the Unity Editor during play mode, so I know the animation itself is working.
-
Error: I’ve received an error saying GoalText’ AnimationEvent has no function name specified!. I tried checking the Animation window and found an empty AnimationEvent, but even after removing it, the animation still doesn’t trigger in-game.
-
Animator Trigger: In my GameplayUIManager.cs script, I have an Animator variable called goalText, and I’m using goalText.SetTrigger(“GoalScored”) to trigger the animation. However, this doesn’t seem to work when the goal event is triggered.
-
Puck Respawn: I suspected that the puck respawn might be interfering with the animation, so I removed the respawn logic from the Goal.cs script temporarily, but the issue persists.
-
Disappearing GoalText: At one point, the GoalText UI element disappears in play mode, but it is visible in the scene view outside of play mode. I’m not sure if this is related to the issue.
Scripts
GameplayUIManager.cs
using UnityEngine;
public class GameplayUIManager : MonoBehaviour
{
private static GameplayUIManager _instance;
[SerializeField] private Animator goalText;
public static GameplayUIManager Instance => _instance;
private void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public void OnGoal(Player winner)
{
if (goalText != null)
{
goalText.SetTrigger("GoalScored"); // Triggering the goal animation
}
}
public void ResetGoal()
{
if (goalText != null)
{
goalText.ResetTrigger("GoalScored");
}
}
}
Goal.cs
using UnityEngine;
public class Goal : MonoBehaviour
{
[SerializeField] private Player owner;
[SerializeField] private Puck puck;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Puck"))
{
Score();
}
}
public void Score()
{
Debug.Log("Goal Scored by: " + owner);
// Trigger goal animation in the UI
GameplayUIManager.Instance.OnGoal(owner);
}
}
Puck.cs
using UnityEngine;
public class Puck : MonoBehaviour
{
private Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
public void ResetPuck(Vector2 startPosition)
{
rb.velocity = Vector2.zero;
transform.position = startPosition;
}
}
Steps I Have Tried:
- I verified that the animation works in the Animator window by manually triggering it during play mode.
- I ensured the Animator controller has a trigger parameter called “GoalScored” and that the transition from the idle state to the goal animation is set up.
- I temporarily removed the puck respawn logic to rule out interference with the animation trigger.
- I tried placing the GameplayUIManager on different GameObjects to ensure it wasn’t being destroyed or interfering with the goal script.
Question:
Given that the animation works in the Editor but not when triggered by the scripts, what could be preventing the animation from playing in-game? Could the disappearing GoalText UI object or some issue with the Animator setup be causing this? What else should I look into or try to resolve this issue?