How to make bullets instantiate?

Godot 4.3

I currently have a bullet system set up to try and make the bullet scene spawn and instantiate when the action is pressed. But it doesn’t seem to work properly.

PLAYER CODE:

func _process(delta):
if Input.is_action_just_pressed(“bullet”):
bullet()

func bullet():
var bullet = Bullet
owner.add_child(bullet)
bullet.transform = $bulletspawn.global_transform

BULLET CODE:

class_name Bullet extends Area2D

var SPEED = 750

func _physics_process(delta):
position += transform.x * SPEED * delta

func _on_Bullet_body_entered(body):
if body.is_in_group(“enemy”):
body.queue_free()
queue_free()

What does the “Bullet” var refer to in your code? Is it a preloaded Bullet scene?

1 Like

Bullet in this case refers to your GDScript class, which is not an entire scene. If this was created correctly, like so:

var bullet = Bullet.new()

Then you would only get a Area2D with no children such as a Sprite2D or CollisionShape2D. You need to load/preload your bullet scene and .instantiate() that.

var bullet = preload("res://Bullet.tscn").instantiate()

thank you very much!