This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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: https://goo.gl/i4VvyQ
Here is the Spawn Menu Tree: https://goo.gl/Jwm82t

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)
in Engine by (164 points)
edited by

1 Answer

+1 vote
Best answer

You need to get the spawner position.

Godot 3
bathtubSpawn.set_position(spawner.get_position())

bathtubSpawn.position = spawner.position

Godot 2
bathtubSpawn.set_pos(spawner.get_pos())

by (618 points)
selected by

Doesn't work, it doesn't spawn anything, but it does register the button press

I'm sorry, I make a mistake.

You should use get_global_position().

bathtubSpawn.set_position(spawner.get_global_position())

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 Frequently asked questions and 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 [email protected] with your username.