- Home /
there is no 'animation' attached to the gameobject but a script is trying to access it
I have an animation called "Swing" attached to an object called FreeCharacter_01 and a script that tells it to play when the Mouse0 button is pressed. Both of these, the animation and the script, are both attached to FreeCharacter_01, but when I hit the left mouse button, it says "there is no 'animation' attached to the gameobject but a script is trying to access it". I've searched all over the internet and other forums to find the answer and all of them just don't work out. I can't figure it out? Any ideas? (P.S. I am a massive noob) Also, here's the script.
#pragma strict
function Update()
{
if(Input.GetMouseButton (0))
{
animation.Play("Swing");
}
}
Copy the animation. Lip in the array on the animator as the first element and this should work.
Are you sure that the correct "swing" animation assigned to the object?
It always helps if you show us the actual error 9and any stack trace if there is one) ins$$anonymous$$d of paraphrasing it.
At a wild guess, your object is actually made up of multiple objects and your script is on one child and the animation component on another.
Another wild guess is you are confusing animation, which is the legacy system, with animator which you need to use $$anonymous$$echanim to use.
A third wild guess, your import of the animations is set wrong in your model importer.
Did you ever figure this out? I'm getting the same error. (And that's exactly what the error says)
Answer by HD32 · May 04, 2015 at 10:20 AM
Here you go sir:
var walkClip: AnimationClip;
var anim: Animation;
function Start() {
anim = GetComponent.<Animation>();
anim.AddClip(walkClip, "walk");
}
I hope that helped. If you wanted to use C# here is the code:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public AnimationClip walkClip;
public Animation anim;
void Start() {
anim = GetComponent<Animation>();
anim.AddClip(walkClip, "walk");
}
}
All of that is from the scripting API, so it should work. Just put incorporate that into the code you have right now and it should import the animation for use along side the script. =D
Answer by Crystalline · May 04, 2015 at 09:19 AM
The animation you are using is more than sure "legacy" type, in newer unity versions this wont work unless you mark it as legacy.
Solution:
Select animation,go into debug mode, there is a little button with three lines in the right corner of the inspector , and then select "Legacy".
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
animated sword + script 1 Answer
Swinging a sword without an animation... 1 Answer
Walking animation problem. 0 Answers
Animation Not Playing 1 Answer