- Home /
Error... that I don't understand.
I have a very simple script set up, and I am getting two errors. The errors are:
Assets/Movement.cs(15,26): error CS1502: The best overloaded method match for 'UnityEngine.Input.GetButton(string)' has some invalid arguments
Assets/Movement.cs(15,26): error CS1503: Argument '#1' cannot convert
UnityEngine.KeyCode' expression to type 'string'`
My script is here:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float speed = 4f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButton(KeyCode.Space)) {
transform.Translate(Vector3.up * Time.deltaTime * speed);
} else {
transform.Translate(Vector3.down * Time.deltaTime * speed);
}
}
}
Thanks in advance! :D
Answer by Soffry · Mar 01, 2014 at 04:31 PM
Try to change from
if(Input.GetButton(KeyCode.Space))
to
if(Input.GetButton("Space"))
In fact Input.GetButton requires a string (e.g. "Space") as a valid value.
Answer by Linus · Mar 01, 2014 at 04:27 PM
GetButton takes string not a KeyCode. Name the inputs in the input manager.
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButton.html
Answer by robertbu · Mar 01, 2014 at 04:29 PM
You want Input.GetKey().
if(Input.GetKey(KeyCode.Space)) {
Input.GetButton() returns a named button from your defined Axes and must be passed a string name.