GDScript - Collision Shape not being set

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DragonOfWar

I’m trying to create automatically set the size of a collision shape through GDScript, so I don’t have to set it directly. The problem is that the shape I’m creating is not being set to the CollisionShape. Here’s the code:

func _ready():
var collision = $CollisionShape
var shape = SphereShape.new()
shape.radius = view_distance

collision.set_shape(shape)

I’ve also tried setting the radius of a SphereShape already assigned to the CollisionShape, and creating a CollisionShape, then setting the shape to it. None of those worked.

Edit: I’ve noticed that setting a Shape to the CollisionShape with one already assigned to it, it will set the shape parameter to empty

:bust_in_silhouette: Reply From: wombatstampede

This looks basically correct but if you just want to set the radius then you could do this: (if there’s already a SphereShape assigned to CollisionShape)

$CollisionShape.get_shape().radius = view_distance

Make sure, that $CollisionShape is actually a child of the node which the script is assigned to (if not, there should be some error visible in the log/output).

Hello. I’ve already tried that method, but it didn’t work. I’ve also made sure that the CollisionShape is a child of node, and it is. enter image description here

DragonOfWar | 2019-12-18 16:15

How did you verify that?
Did you simply read the radius value back and print it out? Or did you check via the “visible collision shape” debugger function? It may actually be that the debug output (i mean visible collision shapes, not the print) doesn’t update properly when collision shapes are updated “on the fly”.

wombatstampede | 2019-12-18 16:50

Oh sorry, I was actually verifying it using “visible collision shape” debugger and using breakpoints, both of which were displaying the collider’s original radius, while the collision detection is working how it supposed to. They should probably fix this issue. Thanks.

DragonOfWar | 2019-12-18 17:03