I am trying to play an animation clip through code. I have it so that when the user throws a ball and hits the target, it will play the animation of the body being dunked. The target animates when it hits, but whenever I try to have the body be animated, it dosen’t do anything or I get the error, animation file not found. Even though I set it to legacy. I’ll post some code to show what I am trying to do. Also, note that is a 2D game. The animations were made as clips in the Unity animation editor.
I also discussed with a friend about the same problem, and we think that the animation files you are now creating are not compatible with the Animation Component any more. Also using Animation Component is up-to-date anymore. try using the Animator.
Hmm, that still wouldn’t make sense as one of the animation clips works and it was made with Unity’s animation editor. Also, the animations have animator classes/objects, I forget what they’re called, but the animations do have those. I mostly need to try and get the animations to play from code. Thanks for the suggestion though
Also had the problem with a simple rotation of an object animation using the Animation Component. Just to check if it’s working but got the same error like you.
But we using Animator System from Unity so we don’t have this problems anymore.
I see. Well I don’t think using the Animator would make much of difference, I’m just trying to get it to play the animation clips in code. One of the clips works just fine, but the others aren’t for some reason. So I don’t think its directly related to the animator thing. I do appericate the help and suggestions, though.
It’s not at all related to the Animator thing. Animation and Animator are two completely different systems that don’t really have much overlap. The Animation system is deprecated for a reason, but being hit with nothing but a barrage of “just stop using that system” probably isn’t helping much.
I’m guessing that you have capitalization problems or you’re looking for the animation in the wrong spot. That said, the little piece of code you posted tells me absolutely nothing. Take screenshots of the animation in your project that worked (settings and where it is in your directories), and one of an animation that isn’t working, along with the script you’ve written to try and play both of them.
using UnityEngine;
using System.Collections;
public class Target : MonoBehaviour {
// Use this for initialization
Animator Splash;
void Start () {
Splash = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.CompareTag ("ball")) {
// SEND MESSAGE HERE
Debug.Log("TARGET HIT: trigger animation and poin increase here");
this.GetComponent<Animation>().Play("target_hit");
//this.GetComponent<Player>().dunks++;
//this.GetComponent<Animation>().Play("SeatFlip");
SoundManager.Instance.PlaySound("splash");
//Splash.Play("Splash_2");
//this.GetComponent<Animation>().Play("LightWomanAni");
// ignores collision with ball after being hit once
// (to prevent multiple hits from the same ball)
Physics.IgnoreCollision(this.GetComponent<Collider>(), col.collider);
}
else if( col.gameObject.CompareTag("Doofus1"))
{
this.GetComponent<Animation>().Play("DoofusAni");
}
else if(col.gameObject.CompareTag("Donnie1"))
{
this.GetComponent<Animation>().Play("DonnieAni");
}
else if(col.gameObject.CompareTag("lightwomansuit1"))
{
this.GetComponent<Animation>().Play("LightWomanSuitAni");
}
else if(col.gameObject.CompareTag("darkwomansuit1"))
{
this.GetComponent<Animation>().Play("LightWomanSuitAni");
}
else if(col.gameObject.CompareTag("lightmansuit1"))
{
this.GetComponent<Animation>().Play("LightManSuitAni");
}
else if(col.gameObject.CompareTag("darkmansuit1"))
{
this.GetComponent<Animation>().Play("DarkManSuitAni");
}
else if(col.gameObject.CompareTag("lightwoman1"))
{
this.GetComponent<Animation>().Play("LightWomanAni");
}
else if(col.gameObject.CompareTag("darkwoman1"))
{
this.GetComponent<Animation>().Play("DarkWomanAni");
}
else if(col.gameObject.CompareTag("lightman1"))
{
this.GetComponent<Animation>().Play("LightManAni");
}
}
}
This is the entire code for the animations to be played. I have the animations that need to be played in the Assets folder that is included when making a project in Unity. I also made sure to check and see the names were put in correctly. I also have them set to Legacy.
In the “Animation” component attached to the character, there’s a list called “Animations”. Do you have all of these different animations added to that list? Also, I see you added an “Animator” component to the same object. You should probably delete that- you really can’t be using both systems at the same time.
Yes I have all of the animations added to the list. I’ll get rid of the animator line. This is my first game in Unity and I’m having trouble with the animation part. Do I need to add the animations to the speicific game object, like the target? Or do I neeed to make a prefab of the animations and then have it play the prefab of animations?
The animations need to be added to the “animation controller”, in that list I mentioned, for each character/GameObject that needs to play those animations. When you call GetComponent it’s grabbing the animation component for the current GameObject (whatever this script is attached to). Then, when you use .Play to try and play an animation, it needs to be in the list for this animation component for this GameObject.
If it’s telling you that the animation doesn’t exist, then you’re either mispelling the animation clip name, getting the capitalization wrong, or they’re just not in the list. That’s literally all I can think of, but as I haven’t used the old animation system in a long time, there are probably other things that could be going wrong that I just don’t know about.
Ah I see. Now its making more sense. Hmm is there a way to play the animations without having them attached to a gameobject? We made the animation files, we just need to play them after a certain event has been reached, after the player hits the target with the ball. I double checked and made sure the names were right and I know the files exist. I even made sure to add them into the list.
I had them attached to the target gameobject, but I don’t think that is the way to go about.
Why would you need to play them without attaching them to the GameObject? You should consider the animation clips “possibilities”. When you’re going through the development process, you attach all of the possible animations you might want to play to the character right off, then if something happens in the scene that’s going to force the character to play one of those animations, all they have to do is call a function on that character called, for instance PlayAnimation(animationName), or animationName can be an enum of possible animation types or whatever. Inside of that function, the character is the one that actually calls animationComponent.Play(“Some Animation”) on itself. An object should handle its own animation, always, even when animation state changes (or clip changes I guess, using the old system) are technically being forced on it.
The answer to your question is “probably, yes” you likely can play an animation without actually “attaching it” to the GameObject, but then you’d have to store the information somewhere, and it makes no sense to store information about a character anywhere except in a script attached to the character- that’s what the component system is for.
Sorry I forgot to mention that. I do have the animation clips attached to the game body objects,the characters. However I think the error may be because I am trying to play the animation from inside the target script, but the target script is attached to the target game object. Perhaps I need to attach the target script to the character gameobject? Or I need to make a separate script that just plays the animation and then have that script be called from another script.
You can leave the script where it is and call a function on the character object instead, which then uses the animation on itself, as I mentioned. This would be the best way.
However, as a shortcut (and bad practice), just change all of your “this.” calls in your script to “col.gameObject” and leave the rest the same.
OK, thanks. Now its saying it could not play the animation state because it could not be found, but I know the file exists and I know I put the name in correctly.
void OnCollisionEnter(Collision col) {
if (col.gameObject.CompareTag ("ball")) {
// SEND MESSAGE HERE
Debug.Log("TARGET HIT: trigger animation and poin increase here");
this.GetComponent<Animation>().Play("target_hit");
//this.GetComponent<Player>().dunks++;
//this.GetComponent<Animation>().Play("SeatFlip");
gameObject.GetComponent<Animation>().Play("LightWomanAni");
SoundManager.Instance.PlaySound("splash");
// ignores collision with ball after being hit once
// (to prevent multiple hits from the same ball)
Physics.IgnoreCollision(this.GetComponent<Collider>(), col.collider);
}
}
The animation state LightWomanAni could not be played because it couldn’t be found!
Please attach an animation clip with the name ‘LightWomanAni’ or call this function only for existing animations.
UnityEngine.Animation: Play(String)
Target:OnCollisionEnter(Collision) (at Assets/Scripts/Caleb/Target.cs:32)
This is the updated code and a pic showing that the name is correct and that its an animation file.
You’re still calling it on the CURRENT GameObject- if this script is attached to the ball, then you need to call it on the collided-object (player) using “col.gameObject().Play()”
OK I think I know what might be the problem. I think its because the game body object is not set to active. So if its not active, then it can’t read from the animation component if the gameobject itself is not active.
That’s true- a GameObject being inactive will not run Update or any other scripts or functions, with the exception of Triggers (so that the Trigger can cause the object to activate itself).
Use OnCollisionEnter instead? If you check “isTrigger” in the collider, then it won’t physically stop colliding objects from passing and you can use OnTriggerEnter/Stay/Exit. If you want the object to physically stop, then you use OnCollisionEnter/Stay/Exit and uncheck “isTrigger”. If you do not check “isTrigger”, then the trigger functions won’t run at all.