How do I fix error cs0619? Error CS0619: `UnityEngine.Component.rigidbody2D' is obsolete: `Property rigidbody2D has been deprecated. Use GetComponent() instead. (UnityUpgradable)' (CS0619) (Assembly-CSharp)

I am trying to make a sprite move but when i tried to implement my code it came up with the cs0619 error . How can I fix it?

My code:

using UnityEngine;
using System.Collections;

public class Player : Entities {

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKey(KeyCode.W))
		{
			rigidbody2D.transform.position += Vector3.up * speed * Time.deltaTime;
		}
		if(Input.GetKey(KeyCode.S))
		{
			rigidbody2D.transform.position += Vector3.down * speed * Time.deltaTime;
		}
		if(Input.GetKey(KeyCode.A))
		{
			rigidbody2D.transform.position += Vector3.left * speed * Time.deltaTime;	
		}
		if(Input.GetKey(KeyCode.D))
		{
			rigidbody2D.transform.position += Vector3.down * speed * Time.deltaTime;
		}
	}
}

2 Answers

2

Use:

GetComponent<Rigidbody2D>().transform.position += Vector3.up * speed * Time.deltaTime;

Instead of:

rigidbody2D.transform.position += Vector3.up * speed * Time.deltaTime;

This was changed in Unity 5. You could also do it like this.

public Ridgidbody2D rb;

 // Use this for initialization
 void Start () {
       rb = GetComponent<Ridgidbody2D>();
  }

 // Update is called once per frame
 void Update() {
      rb.transform.position += Vector3.up * speed * Time.deltaTime;
 }