Access different rigidbody2D

So I’m currently working on a different variation of Block Breaker with some power blocks and there is one block that enable player to shoot
So the bullet has a Rigidbody2D component but at the same time, the ball also has a Rigidbody2D component so my question is: Can you use FindObjectOfType<> to find the Rigidbody you want to work with?


You can see here it’s the bullet that moves on click instead of the ball

Hi,

You should really limit the use of any of the “Find” methods, they are typically slow and will effect performance.

It sounds as if your issue may be more of an architecture one. I’m uncertain how you have things wired up, but the behaviour for moving the bullets should be on the bullets, you shouldn’t need to find a bullets Rigidbody2D component.

1 Like

I got it. Thank you!
Now how can I make the bullet disappear upon collision with the block and avoid its collision with the ball?
Thank you

Hi,

You could either check within the OnCollisionEnter2D method to determine what had made the collision and then only act where necessary, or, use layers. Assigning different layers to the ball and the bullets, and within the Project Settings / Physics 2D enable/disable the detection of collisions between specific layers.

Hope this helps :slight_smile:

On a related note, it’s better to copy/paste your code into your posts and apply the code formatting characters before and after it rather than using screenshots. Screenshots tend to be less readable, especially on mobile devices where the user will need to zoom in and scroll around. You also prevent those that offer to help you from copy/pasting a part of your code back to you with suggestions/corrections. If you want responses, make it easy for people to respond :slight_smile:


See also;

Privacy & Terms