- Home /
Animation plays (is visible) in scene but not in game
In a 2D test project (exploring Unity) I have a Prefab I intend to use in each scene that plays an animation on touch/click. The Prefab is a game object that has an Animator that animates the size and opacity of the sprite. When clicked a trigger is triggered that causes the animation on the object to start. The script is attached to the Prefab.
The script is below.
public class TouchControls : MonoBehaviour {
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
Vector3 position = Input.mousePosition;
gameObject.transform.position = Camera.main.ScreenToWorldPoint(position);
anim.SetTrigger("touch");
}
}
As you can see below the scene shows the halo being animated in the scene but nothing shows up in the game. I've messed around with the ordering and layers to no effect. Am I missing something obvious here? Is there a good way to debug such a thing? I've even shut off the background and it still does not appear in the game preview.
After looking at the sprite during the game I realized the z value was getting changed to -10. This is because I was setting the position to the mouse position which I assume is set away from the scene. To resolve this I only update the x and y position.
Hi,
I have the same problem. I am making a 2D game and I have created animations using sprites. Animation is visible in Scene window but not in Game window. I have checked the z values, they stay at zero during game play.
Any help would be highly appreciated.
Thanks,