Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MCRocks999 · Apr 03, 2015 at 03:47 PM · unity5player movement

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Apr 03, 2015 at 04:22 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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));
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MCRocks999 · Apr 03, 2015 at 05:10 PM 0
Share

Thank you. I'm hoping to keep using Unity 3d then. :D

avatar image
1

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>().

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MCRocks999 · Apr 03, 2015 at 04:11 PM 0
Share

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.

avatar image supernat · Apr 03, 2015 at 04:15 PM 0
Share

Sorry, I forgot to use HT$$anonymous$$L when putting the greater than/less than signs in, see my updated answer.

avatar image Hellium · Apr 03, 2015 at 04:16 PM 0
Share

As supernat said, avoid using the GetComponent function with a type as parameter. Try out with this syntax :

 GetComponent<Rigidbody2D>()

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges