Nonexistent function 'add_child' in base 'PackedScene'

Godot Version

godot 4.2.2

Question

Was following a tutorial for adding new bullets, in my case. The ‘add_child’ is not liking any variation I’ve done (such as instancing the scene, then using $Bullets.add_child(b)). Thanks in advance.

extends CharacterBody2D

@export var muzzle_velocity = 350
@export var gravity = 250
var Bullets = preload(“res://bullet.tscn”)

func _process(_delta):
if Input.is_action_pressed(“shoot”):
shoot()

func shoot():
var b = Bullets.instantiate()
Bullets.add_child(b)
b.transform = $Player / Position2D.global_transform
b.velocity = b.transform.x * muzzle_velocity
b.gravity = gravity

in Bullets.add_child(b), Bullets is a PackedScene. It’s not a node, but just a reference to a scene in your files. It should probably be replaced by self.add_child(b) or whatever node you want to be a parent to your bullets.

1 Like

Awesome, thank you!
Ended up making a Marker2D node and used $Marker2D.add_child(b)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.