I have the following script on my water object which, when the Players ship triggers it, the ships gravity is inverted to give the sense of buoyancy. I have added a 'print' statement to prove that the collision is working and that the Name condition check works, which it does. However it throws an error when trying to access the plays ship Rigidbody2D property to then access the GravityScale with the message:
There is no 'Rigidbody2D' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Rigidbody2D to the game object "Player"...
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.name == "Player")
{
print("Player Hit Water");
Rigidbody2D tmp = coll.gameObject.GetComponent<Rigidbody2D>();
tmp.gravityScale *= -1;
}
}
Update: It turns out this code is fine it was the structure of my gameobjects that was the problem as the Rigidbody2D was in the child object, not the parent. See comments below for details.