Accessing RigidBody2d from OnTriggerEnter2D(Collider)

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;     } } 

Replay

Category: unity Time: 2016-07-31 Views: 0
Tags: events unity

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.108 (s). 12 q(s)