Node.new() not instantiating children

Godot Version

4.2.1

Question

I’ve been trying to get this to work for days, but nothing seems to like me.

I’m instantiating a scene with node.new(), but it doesn’t seem to instantiate any of the children of the node.

This is all the code for the ship (ship_base_2) I’m instantiating.

extends CharacterBody2D
@onready var nav_agent : = get_node("./NavAgent")

func _ready():
	print(get_children())

And this is the code used to instantiate the ship, set up to a specific node in my world.

extends Node2D

@onready var ship_base: = preload("res://ship_base_2.gd")

func _process(delta):
	if Input.is_action_just_pressed("mouse0"):
		var new_ship := ship_base.new()
		add_child(new_ship)

Here’s the output in the scene:


the first line is a ship already in the scene, and the next two were instantiated with ship_base.new()
Where have I gone wrong??? I’m tearing my hair out because this issue has been following me through entirely different projects for a while now.

Preload returns a PackedScene (when preloading a scene.)
You want to add an instance() of that PackedScene.
var new_ship := ship_base.instantiate()
But in my opinion it is better to actually specify the type of the variable instead of using :=; in this case it would have thrown an error and you would have had no problem sorting it out.

1 Like

Thank you so much for responding, helped me realise that the real issue was actually that I should have used “res://ship_base_2.tscn” instead of preloading the SCRIPT fghsfagfb :grimacing:

1 Like

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