Object spawn on the floor

Godot Version

Godot 4

Question

Character has var floor_registered = null, a new floor is registered every time character touches a new one
adssads

Then a murder can happen and the blood should spawn and connect as a child to the floor that was last registered
Снимок

But godot says: Invalid call. Nonexistent function ‘add_child’ in base ‘Nil’.
What should I do?

Maybe the floor_registeted is really null? I observed you had set it only if any CollisionPolygon2d should enter, but the area only detects any static or rigid body not a single collision shape.

1 Like

Using the name “CollisionPolygon2D” to check if it was a floor you touched is not the most reliable. First of all, I think it’s not the collision polygon that gets passed to _on_body_entered, but its parent (probably a staticbody). Second, what if at some point you make one with a different name?

A better solution would be to use either collision layers or groups, since that should at least prevent spelling-related problems. It’s still possible to forget to set the layers/groups on a part of the floor, but I don’t know of a way to completely prevent that…

2 Likes

Снимок

Ok, I added RigidBody2D, now it works