How to instantiate and set the object to specific node child?

Godot Version

4.2.2 stable


I have managed to instantiate the object now I’m trying to get the camera to that object but I have problems:

my code: for instantiation:

extends Node

func _ready():
	for n in container.size():
		dictCon[container[n].entityNum] = container[n]
	initialize = true

var dictCon = {}
var initialize = false

@export var container: Array[Entity]

func CreateMe(index: int):
	if not initialize:
		await get_tree().process_frame
		var res = dictCon[index]
		if res.scene == null:
			printerr(String("index: [{i}] selected Entity: [{e}] does not have a scene\n{stack}").format({i = index, e = res.entityString, stack = get_stack()}))
			return res.scene.instantiate()
		printerr(String("wrong index: [{i}] selected Entity does not exist\n{stack}").format({i = index, stack = get_stack()}))

where I call the instantiation:

func _ready():
	GameManager.planetPosition = position
	GameManager.planet = transform
	print("planet ready")
	await get_tree().process_frame
	print("planet ready 2")
	#var item = await DataBaseResources.CreateMe(1)
	var c = await MP.CreateMe(2)
	var s = c.get_script()

camera’s script:

extends Camera3D

func _ready(): = self

player’s script which does not want to get fiered at the s.SetCamera() but there I don’t get any errors

func _ready():

func SetCamera():
	camera =
	camera.position = cameraPivot.position
	initialized = true

it works without errors but camera doesn’t want to be attached

I’ve tried other way around too:


try to print in set camera function

but only if I have in player script: SetCamera()

to print in Player’s script:




Try to do this:

func SetCamera():
	camera =
	#camera.position = cameraPivot.position not set it...
	initialized = true

nothing it does not attach to the pivot of camera of player’s body

and it stays from where it was positioned at start scene which is logical because position is not set.

Have you tried this?

yes copied and pasted the code

tried and same it does not attach

When you run the game, in the scene tree where all the nodes in the editor, you will see two options, “remote” and “local”, so there, press the “remote” and see what happened, is the camera inside the game?

can’t find remote/local

but I know that camera3D is inside root of map

just check everything in the remote like pos, etc:

I marked it, when you run the game, minimize it and check the editor

holy crap it seems I don’t have it

First run the game, then you can see it in the editor, do not stop the game

yes camera is inside game but not inside the body, … but doesn’t move, weird thanks for this didn’t know that I can inspect like that

1 Like

Ok so I think you forgot to move the camera :sweat_smile:

ok I inspected it closely it didn’t go in the body but it’s inside the game

in both cases add_child

it’s not inside the body

like it doesn’t move

Means camera is not added in game with codes?

weird after trying to add_child

I get


That means the camera added to scene, so in remote the camera is in? Or the camera position is following the target?

camera is not following the target because it’s not under node of body

the camera in remote is under root of map

What is camera pivot, is it not the body?:

func SetCamera():
	camera =
	cameraPivot.add_child(camera) #What is it?
	camera.position = cameraPivot.position
	initialized = true