- Home /
Expression denotes a `type', where a `variable', `value' or `method group' was expected
This makes no sense.
(First time using Unity 5)
Here's my code of Player.cs: using UnityEngine; using System.Collections;
public class Player : Entity {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.UpArrow)) {
GetComponent(Rigidbody2D).transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.LeftArrow)) {
GetComponent(Rigidbody2D).transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.DownArrow)) {
GetComponent(Rigidbody2D).transform.position += Vector3.down * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.RightArrow)) {
GetComponent(Rigidbody2D).transform.position += Vector3.right * speed * Time.deltaTime;
}
}
}
please help.
You should avoid using $$anonymous$$eyCodes, especially if you are moving a first person character.
Prefer the key identifiers in the Input $$anonymous$$anager (like "up", "down", "left", "right"). It's more flexible, and you will have the possibility to quickly remap your keys like you want.
See documentation : http://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$ey.html
Answer by Neamtzu · Apr 03, 2015 at 04:29 PM
Why do you get the rigidbody component of the current gameobject when you can access the transform of the current gameobject directly? Try something like this:
transform.position += Vector3.up * speed * Time.deltaTime;
GetComponent method takes a type as a parameter. To express a type in C# you must use the "typeof" keyword. Here is the correct format:
GetComponent (typeof(Rigidbody2D));
Answer by supernat · Apr 03, 2015 at 03:49 PM
I assume the error describes the line number(s) which would be helpful, but one thing I see is that you are calling GetComponent(Rigidbody2D) instead of GetComponent<Rigidbody2D>().
Oh line 14 so line 11 on what I've copied. Also I have no idea how to use Unity 5 that much. Thing is; I have a component on my player called Rigidbody 2d and I'm trying to move my player with it.
Sorry, I forgot to use HT$$anonymous$$L when putting the greater than/less than signs in, see my updated answer.
As supernat
said, avoid using the GetComponent function with a type as parameter. Try out with this syntax :
GetComponent<Rigidbody2D>()
Your answer
Follow this Question
Related Questions
Why Won't it jump? 1 Answer
Player rotation in Unity 5 0 Answers
how to disable player inputs? 1 Answer
What script should I write for RotationHelper for my code to work? 0 Answers
player following touch position 0 Answers