Problems with Mecanim and Script
Hello, I am about to loose my mind. I'm trying to get it working to press the left arrow results in walking left and playing a running animation I am controlling in Mecanim with a Integer. I managed to get my character working but whatever I do, the animation wont play. For starters, here is my script:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
Animator animator;
float speed = 1.5f;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.left * speed * Time.deltaTime;
animator.SetInteger("velc", 1) ;
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.right * speed * Time.deltaTime;
animator.SetInteger("velc", 1) ;
}
}
}
and here a screenshot of my controller:
Now the animation simple wont play and the idle ("dog1_") animation just keeps playing and I get the following error: MissingComponentException: There is no 'Animation' attached to the "Main Character" game object, but a script is trying to access it. You probably need to add a Animation to the game object "Main Character"
The Problem is both the animator and the script has been attached to the Main Character. I'm highly confused and would appreciate any help, thank you!
Same animation to Left and Right?
Try this:
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow) || Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
animator.SetInteger("velc", 1) ;
}else{
animator.SetInteger("velc", 0) ;
}
$$anonymous$$ake sure to uncheck Loop Time.
O$$anonymous$$, we have the error, but what script and line has that error? Animation and Animator are 2 different components, and the error messages says some script is looking for an Animation component (not pressent in your code).
An exception could make anything to stop working properly, get rid of that first.
That will be another script trying to access Animation both $$anonymous$$e and OP's code has no reference to Animation Component.
@Positive7, yes, sorry, I was talking to OP. Forgot to clarify, it looks like a reply to your answer.
Your answer
Follow this Question
Related Questions
Unity relative animations (Frequently asked but I don't get it) 1 Answer
Need help aligning animations to a top down dual joystick shooter. Plz!! 1 Answer
if(pressbutton) --> do something!! 1 Answer
I want Script To Move a object from a different object (In four Different directions,Randomly) . 0 Answers
I want my script to wait 2 seconds before continue in a condition, in update, using C# 2 Answers