Instanced scenes aren't visible

Godot v4.2.1.stable.arch_linux

Hello !

I’m trying to understand how to instance scenes using the instantiate() method, however, the instanced scene is invisible (the scene being an enemy made out of a CharacterBody3D, a Sprite and Collision3D nodes).

I did print it’s position and type of node, and when I do : print(get_tree.get_node_count), it’s the same value before and after instancing.

What I’d like to know is how to properly instantiate in Godot 4 and what i did wrong.

Here’s the code below :

extends Node3D

#This Node is an enemy spawner
@onready var timer = $"Timer"
@export var enemyScene: PackedScene

#The goal here is to make waves of enemies
@export var firstEnemyTimer : int = 0
@export var secondEnemyTimer : int = 0
@export var thirdEnemyTimer : int = 0

func _ready():
	print(get_tree().get_node_count())

func _on_timer_timeout():
	var enemy = enemyScene.instantiate()
	enemy.position = self.position
	enemy.scale = enemy.scale * 3
	print(get_tree().get_node_count())

instantiate doesn’t cause the new node to be put into the scene-tree. You need to do it separately:

parent_node.add_child(enemy)
4 Likes

Thanks man, it did work ! I tested it out and I can print out it’s position, children and parent, meaning it IS here !

However, I cannot see the node itself while playing : no sprite and no collision box either with “Visible Collision Shapes” active, AND I made sure that his position is in front of the camera. I appreciate the help, but do you know if there is a way to fix this ?

Edit : I did try to use preload("") however it didn’t change anything.

Can you see other objects while playing?
If not, do you have a light and a WorldEnvironment in your scene?

Here’s what i got in the scene, the red cylinder is the spawner (the enemy is larger than it) and the blue lines are just the collision shape for the player. I gave the enemy instance the same position as the spawner.

If we could see the enemy, we’d at least see it’s collision shape (in the case that the sprite was actually too small).

I did add a world environment and a directional light, but no changes.

Okay, I managed to understand why it didn’t work, I gave the enemy’s position the same as the spawner HOWEVER this position isn’t related to the world space but the spawner itself, meaning a position of (0,0,0) for the enemy is the same as placing it on top of the spawner.