Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Bandeh_ · Jul 01, 2016 at 01:25 AM · errorcompiler-error

What is wrong with this code?

Hey guys, so every time i try to play my 2d platformer it says all errors must be cleared before playing exact: Assets/Scripts/PlayerController.cs(17,13): error CS0619: UnityEngine.Component.rigidbody2D' is obsolete: Property rigidbody2D has been deprecated. Use GetComponent() instead. (UnityUpgradable)'

Assets/Scripts/PlayerController.cs(17,25): error CS1061: Type UnityEngine.Component' does not contain a definition for velocity' and no extension method velocity' of type UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

Here is my code for gravity:

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour {

 public float moveSpeed;
 public float jumpHeight;

 void Start () {
 
 }
 
 void Update () {
 
         if(Input.GetKeyDown (KeyCode.UpArrow))
     {
         rigidbody2D.velocity = new Vector2(0, jumpHeight);
     }

     if (Input.GetKey (KeyCode.RightArrow))
     {
         rigidbody2D.velocity = new Vector2(moveSpeed, 0);
     }

     if (Input.GetKey (KeyCode.LeftArrow))
     {
         rigidbody2D.velocity = new Vector2(-moveSpeed, 0);
     }

 }

}

Comment
Add comment
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

2 Replies

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

Answer by Ahndrakhul · Jul 01, 2016 at 02:37 AM

I think you need to get a reference to the rigidbody2D before you use it. Either you can change all of the lines that use rigidbody2D.velocity to GetComponent<rigidbody2D>().velocity, or you can do something like this:

 using UnityEngine; 
 using System.Collections;
 
 public class PlayerController : MonoBehaviour 
 {
      rigidbody2D rB2D;   //<-- rigidbody2D reference
      public float moveSpeed;
      public float jumpHeight;

      void Start () 
      {
          rB2D = GetComponent<rigidbody2D>(); //<-- get rigidbody2D and assign to rB2D
      }
      
      void Update () {
      
          if(Input.GetKeyDown (KeyCode.UpArrow))
          {
              rB2D.velocity = new Vector2(0, jumpHeight);  //<-- use rB2D variable 
          }
          if (Input.GetKey (KeyCode.RightArrow))
          {
              rB2D.velocity = new Vector2(moveSpeed, 0);  //<-- use rB2D variable 
          }
          if (Input.GetKey (KeyCode.LeftArrow))
          {
              rB2D.velocity = new Vector2(-moveSpeed, 0);  //<-- use rB2D variable 
          }
      }
 }





Comment
Add comment · Show 2 · 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 Bandeh_ · Jul 01, 2016 at 01:00 PM 0
Share

Thanks alot!

avatar image NienNumb · Jul 12, 2017 at 03:32 AM 0
Share

Thank you sir.I missed this addendum in the Tutorial. Wasted quite a bit of time due to the fact that my code was exactly the same as in the Tutorial but it didn't work! I also started a questions on this... http://answers.unity3d.com/questions/1377616/error-cs-1061-space-shooter-tutorial-code-for-play.html

avatar image
0

Answer by Raveerojh · Apr 03, 2017 at 07:48 AM

GetComponent(); is not corrected, so should be GetComponent();

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 Raveerojh · Apr 03, 2017 at 07:49 AM 0
Share

GetComponent();

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

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

All scripts in unity give the error "the associated script can not be loaded please fix any compile errors and assign a vaild script" 4 Answers

I cant build or run even when there is literaly nothing created. 1 Answer

Help About this error 0 Answers

Project won't run after updating to Unity 5.6, getting: APIUpdater encountered some issues and was not able to finish 3 Answers

Using a script to auto sort file hierarchy causes error CS2001 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