Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | z80 |
Hello!
It is strange. I try adding scene instances by doing the following in _ready() method of my scene set to auto-load in project settings.
func _ready(): var inst = preload( "res://scene.tscn" ).instance() get_tree().get_root().add_child( inst)
It stays invisible. I can’t see it. But if I do
func _ready(): var inst = preload( "res://scene.tscn" ).instance() self.add_child( inst)
Then everything is fine. Why can it be? What can be wrong with adding child to the root? Godot version 3.2.1.
What is the parent’s class?
Tort | 2020-08-03 12:10
Can you show whole scene tree? Are you sure your inst should be child of the root? Do you have viewports in the tree?
Varden | 2020-08-03 13:22
The whole tree is the following:
Spatial |_ Camera
And the script is attached to the Spatial. The get_tree().get_root() is Viewport, right? And self is Spatial
z80 | 2020-08-03 17:23
Made a few more tests. I’ve replaced the scene root element with MeshInstance to validate.
In the following scene
MeshInstance |_Camera
I can see the Mesh instance via the Camera.
But! If I do get_tree().get_root().add_child( whatever ) or get_parent().add_child( whatewer ) it doesn’t show up. But self.add_child() works no problem.
So, if the get_tree().get_root() was invalid the scene’s MeshInstance also would be invisible. Right? But It is perfectly visible. But when anything is added to the parent of MeshInstance it stays invisible.
z80 | 2020-08-03 17:32
Try to run scene and check here.
It should work as you described. You can share your scene for me to test.
Varden | 2020-08-03 19:56