How to fix "no overload for method get component takes 1 arguments"
Ive got this irritating error which says "No overload for method 'getcomponent' takes 1 arguments.
here is the code
using UnityEngine;
using System.Collections;
public class Character : MonoBehaviour {
public float speed;
public Animator player;
public Animation front;
public Animation frontIDLE;
public Animation back;
public Animation backIDLE;
public Animation left;
public Animation leftIDLE;
public Animation right;
public Animation rightIDLE;
// Use this for initialization
void Start () {
player = gameObject.GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
//here is where I get all the errors v
front = gameObject.GetComponent<Animation> ("WalkFront");
frontIDLE = gameObject.GetComponent<Animation> ("WalkFrontIDLE");
back = gameObject.GetComponent<Animation> ("WalkBack");
backIDLE = gameObject.GetComponent<Animation> ("WalkBackIDLE");
left = gameObject.GetComponent<Animation> ("WalkLeft");
leftIDLE = gameObject.GetComponent<Animation> ("WalkLeftIDLE");
right = gameObject.GetComponent<Animation> ("WalkRight");
rightIDLE = gameObject.GetComponent<Animation> ("WalkFRightIDLE");
//here is where I get all the errors ^
}
void WalkFront (){
if (Input.GetKeyDown (KeyCode.W)) {
front.Play ();
transform.Translate (Vector2.up * speed * Time.deltaTime);
}
if (Input.GetKeyUp (KeyCode.W)) {
front.Stop ();
frontIDLE.Play ();
}
}
}
im a beginner so plz help and quick!!!
As the error says, you can't pass an argument (your "WalkFront", "WalkBack", etc strings) into the generic version of GetComponent. What exactly are you trying to do there? What do those strings represent?
The call in Start() is correct (notice, no argument). Like that, the call simply returns the Animator component found on the current game object (or null if none exists).
Again, what are you trying to do in the offending section of code?
Answer by lockay · Sep 05, 2016 at 11:17 PM
I can see what you are trying to do, but there is an easier way.
You are trying to make a variable that holds each clip, and then tell that clip to play. What you should do is access the Animation component on the GameObject, and use it to play animations.
The very first thing you need to do is add an Animation component to the GameObject. Then, adjust the array and drag in all of the animation clips. It should look something like this: UPDATE: Uncheck "Play Automatically", unless you want the animation to run at start.
Don't add change the first variable, the one that says "Animation." Just leave it blank.
You should also add an Animator component to the GameObject, but you can leave it as is.
Now the code: First make a variable that represents the animation component. This isn't absolutely necessary, but it will make everything easier;
public Animation anim;
In start, set the value of anim:
void Start(){
anim = GetComponent<Animation>();
}
You don't need the GameObject when getting the component because you are already accessing the object that the script is on.
Now, you can tell the animations to play:
void WalkFront(){
if(Input.GetKeyDown(KeyCode.W)){
anim.Stop("WalkFrontIdle");
anim.Play("WalkFront");
}
if(Input.GetKeyUp(KeyCode.W)){
anim.Stop("WalkFront");
anim.Play("WalkFrontIdle");
}
}
Make sure that the animation clip name are spelled EXACTLY as they are in the inspector. WalkFront is different from walkFront and Walk Front.
Now repeat the previous code for walking backwards, sideways, etc. The WalkFront function won't actually run yet. It needs to be called in Update.
void Update(){
WalkFront();
WalkRight();
WalkLeft();
WalkBack();
}
Now the functions will run.
You may get an error similar to 'Animation clip "WalkFront" could not be found'. The main reason that I get the problem is when the clip isn't "Legacy". To make a clip Legacy, click on it in the Project/Assets, then change the Inspector mode to Debug, and check Legacy.