How to get child nodes of an instance in script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By offbeatoctopus
:warning: Old Version Published before Godot 3 was released.

I have three instances in my scene. Each of these instances are different sizes and I was trying to programmatically resize the CollisionShape2D for each instance. However, I noticed that all the instances share the same size as the last instance in my scene.

I am using the following code to change the CollisionShape2D in the instance node.

get_node("Area2D/CollisionShape2D").get_shape().set_extents(Vector2(scale * 10.0, 5.0))

But this seems to target all the CollisionShape2Ds in the scene, rather than just the specific instance to which it is attached.

How do I target the specific instance in this case?

If you duplicated the instances after assigning the collision shape they share the same collision shape, manually create a unique collision shape for every instance.

davidoc | 2017-08-20 16:09

Thanks, that solved the problem.

offbeatoctopus | 2017-08-21 11:46

:bust_in_silhouette: Reply From: quijipixel

Remember that the CollisionShape2D nodes were created as helpers of the editor, so all the instances of the same node will share the same shape (as the shape is defined in the Physics server in runtime, and the node has no more purpose). Check this post, there you can see how to add a shape to a Area2D. Maybe is a better approach to add the collision in runtime rather than creating the CollisionShape2D node in the editor.

Now that I look at the documentation, I did miss that it says its a editor only class.

offbeatoctopus | 2017-08-21 11:50