CollisionShape2D changes for every instance with every instance

Godot Version

4.3

Question

Hi,

I have a little game, where I want to drop instances of a ball (RigidBody2D). My setup looks like this:
image
And I am adding some randomness to the size of the balls and then changing the CollisionShape2D to that random-ish shape like so:
image
However, every time I make another instance of this ball, the CollisionShape2D changes for every instance currently present in the main scene.

I read on the internet that I should make the CollisionShape2D unique, but that did not work at all.

Thank you all in advance :slight_smile:

I think you misunderstand, what needs to be unique is the shape resource inside the CollisionShape2D node (resources are shared by default). You need to create a new instance but change the shape resource to a new unique copy like: $CollisionShape2D.shape = $CollisionShape2D.shape.duplicate(true)

2 Likes

Even though I don’t understand why, putting:

$CollisionShape2D.shape = $CollisionShape2D.shape.duplicate(true)

in the

_ready()

function of the ball scene solved the issue. Thank you :slight_smile:

Setting the duplicate method to true makes that the resources inside that element to also duplicate. By doing so, you created a “unique” version of the shape inside that collisionshape node you duplicated.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.