Problem with the creation of a RigidBody2D and set his position

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

Hi guys,
Is there a way to create a RigidBody2D(maybe with the istance() function) and set his position on the x and y axis?
this is my enemy code:

func _physics_process(delta):
if $"RayCast2D".collide_with_bodies:
 func shot():
var bullet=preload("res://proiettile.tscn").instance()


I tried that but it doesn’t create anything in the level, why?

:bust_in_silhouette: Reply From: njamster

You need to add the instance to the tree as well, using the add_child()-method. This will insert the RigidBody as a direct child node of the scene the script is attached to:

var bullet = preload("res://proiettile.tscn").instance()