How to add textures to a RigidBody2D in Godot?

Godot Version

4.2

Question

How do I add textures to a RigidBody2D in Godot?

I’m working on a 2D project and using RigidBody2D for bullets. I would like to add a texture to the RigidBody2D to represent the bullet visually, but I’m unsure how to properly attach a Sprite2D or another node to display the texture.

Here’s what I’ve tried so far:

I’ve added a Sprite2D as a child of the RigidBody2D.
I made sure the texture is set for the Sprite2D.
In my script, I used get_node_or_null(“Sprite2D”) to access the sprite and set its visibility, but the texture is not showing during gameplay.
Am I missing something in the setup or script? Is there a better way to handle adding textures to a RigidBody2D in Godot?

Thanks in advance!

Can you show your code?
If you are just instancing a bullet scene this would be your structure for the bullet scene:

RigidBody2D
|- CollisionShape2D
|- Sprite2D

You can instantiate your scene like this:

@export var bullet_scene : PackedScene
@export var bullet_spawn_position : Node2D

func instantiate_bullet():
   var bullet_instance = bullet_scene.instantiate()
   get_tree().add_child(bullet_instance)
   bullet_instance.global_position = bullet_spawn_position.global_position

If you you need to change the visibility of your bullet sprite for some reason you would just add this line to you instantiate_bullet() function:

bullet_instance.get_child(1).visible = true 
# or bullet_instance.get_node("name of your sprite2d node").visible = true