+1 vote

Hi Godot masters! I am rather new to gamedev but very enthusiastic. Ran into a little snag trying to instance and addchild. I am getting the error: Invalid type in function 'addchild' in base 'Node2D (Parallax_Example.gd)'. Cannot convert argument 1 from Object to Object.

extends Node2D
var new_house = preload("res://BuildingWalls.tscn")
func _ready():
    new_house.instance()
    add_child(new_house)

I am sure its because I forgot something, just can't think of what it is. The BuildingWalls.tscn is added as a singleton and autoload is checked. BuildingWalls is a scene with a Node2d base with a sprite, polygon2d, skeleton2d & bone2d children. It works if I instance the scene in the editor before runtime.
Thanks to anyone that can point me in the right direction.

in Engine by (16 points)

1 Answer

0 votes
Best answer

The new_house variable is the loaded PackedScene, and then you do add_child(new_house). This is the source of the error.

Your problem is that you're calling instance() to create the scene's nodes but not assigning the result anywhere.

extends Node2D
var new_house = preload("res://BuildingWalls.tscn")
func _ready():
    var house_instance = new_house.instance()
    add_child(house_instance)
by (19,705 points)
selected by

Thanks! That fixed it.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.