How can I make two objects with rigid bodies that won't displace each other?

I have two GameObjects (we’ll call them A and B). Either one of the objects can be controlled by the player at different points in gameplay. Both objects have a 2D boxcollider and a rigidbody to make them controllable and to keep them from going outside the playing field.

The problem is that I want object A to be immovable when object B is in play, and vice versa (object B is immovable when object A is in play).

Currently, if an object in play collides with an object not in play, it will move that object aside. This is unacceptable.

So, how do I make an object controllable, able to collide with other objects, and not movable by other objects?

the only thing i can think of that would be quick and easy would be by turning the objects mass up loads

To make an object immovable, you could do something like this:

		Rigidbody2D rb = GetComponent<Rigidbody2D>();
		rb.bodyType = RigidbodyType2D.Static;

Of course, you should consider cashing the rigidbody2D as a member, since doing GetComponent all the time is expensive.

You could then switch it back to kinematic or dynamic whenever it’s appropriate.