user13657
user13657

Reputation: 765

Unity 2d, access component from code

I have components in my player object like:

enter image description here

How can I acces in PlayerMovement.cs that RigidBody? How can I find it?

Thanks!

Upvotes: 0

Views: 85

Answers (2)

Saif Khan
Saif Khan

Reputation: 309

Create a Rigidbody object like

Rigidbody object;

and use

object = GetComponent<Rigidbody>();

in Start() method. This works for Unity 5

Upvotes: 1

MeshBoy
MeshBoy

Reputation: 692

Main thing is to create a reference variable of type RigidBody like below.

RigidBody rb;

Then you can access the game component by using GetComponant<RigidBody>; function.

So now you can access to the rgid body componant as like rb.mass = 300;

hope this will be helpfull to you.

Upvotes: 1

Related Questions