Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | HarryCourt | |
Old Version | Published before Godot 3 was released. |
I’m trying to make a script where once a button is pressed, it will spawn a prefab into the scene. However, I can’t get it to work and get this message: Cannot convert argument 1 from Object to Vector2
Here is the code:
extends Button
# Items
onready var bathtub = load("res://Prefabs/Items/Bathtub.scn")
# Variables
onready var spawner = get_node("/root/Scene/Player/Spawner")
# Once pressed, the item will spawn on the Players Spawner node
func _on_Button_pressed():
print("Spawned Item")
var bathtubSpawn = bathtub.instance()
bathtubSpawn.set_position(spawner)
add_child(bathtub)
Here is my Player Node Tree: Screenshot - 4507862ac2d605311671d049546ff783 - Gyazo
Here is the Spawn Menu Tree: Screenshot - dc7c28397451b1515f2a62ce8dcc1840 - Gyazo
EDIT: I slightly fixed it, but instead of it spawning on the spawner, it spawns on the Scene Root Pos (0, 0). Here is the new code
extends Button
# Items
onready var bathtub = load("res://Instances/Items/Bathtub.scn")
# Variables
onready var spawner = get_node("/root/Scene/Player/Spawner")
onready var world = get_node("/root/Scene")
# Once pressed, the item will spawn on the Players Spawner node
func _on_Button_pressed():
print("Spawned Item")
var bathtubSpawn = bathtub.instance()
bathtubSpawn.set_position(spawner.get_position())
world.add_child(spawner)