I'm building PacMan game by tutorial and getting error: `Property collider2D has been deprecated. Use GetComponent() instead.' But when I use GetComponent I get another error. What should I use instead?

public float speed = 0.4f;
Vector2 destination = Vector2.zero;

// Use this for initialization
void Start () {
	destination = transform.position;
}

// Update is called once per frame
void FixedUpdate () {
	Vector2 p = Vector2.MoveTowards (transform.position, destination, speed);
	GetComponent<Rigidbody2D> ().MovePosition (p);
	if ((Vector2)transform.position == destination) {
		if(Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
			destination = (Vector2)transform.position + Vector2.up;
		if(Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
			destination = (Vector2)transform.position - Vector2.up;
		if(Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
			destination = (Vector2)transform.position + Vector2.right;
		if(Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
			destination = (Vector2)transform.position - Vector2.right;
	}
	Vector2 dir = destination - (Vector2)transform.position;
	GetComponent<Animator> ().SetFloat ("DirX", dir.x);
	GetComponent<Animator> ().SetFloat ("DirY", dir.y);
}

bool valid(Vector2 dir){
	Vector2 pos = transform.position;
	RaycastHit2D hit = Physics2D.Linecast (pos + dir, pos);
	return (hit.collider == collider2D);
}

// I get error here: return (hit.collider == collider2D);

Hi @igg,

As a noob myself, I am also currently following the noobtuts Pacman tutorial.

I fixed this by just following the error message from the console, which suggested to use GetComponent<Collider2D>(); instead. So I made it work by changing the line in question (28) to:

return (hit.collider == GetComponent<Collider2D>());

I think this has something to do with an engine update that makes components from different classes less dependent on each other and thus speed up online players? I gathered that from this thread.

I also had to move the whole bool function up above FixedUpdate to make it work by the way.

I hope this helps. As I am learning, I come to appreciate reading through all of the line comments and error messages that fly across the screen, as they usually tell you into which direction to blindly poke :wink: