I am trying to make a box spawn on my cursor every 5 seconds but I keep getting an error every time a box should spawn.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Eclipse

I have been making a small program where a box should spawn on my cursor every five seconds but when five seconds pass I get this error

Invalid set index ‘position’ (on base: ‘Vector2’) with value of type ‘Vector2’.

here is my code

extends Node

var box = preload(“res://box/Box.tscn”)

func _on_Timer_timeout():
var pos = get_viewport().get_mouse_position()
pos.position = Vector2(pos.x, pos.y)
add_child(box)

:bust_in_silhouette: Reply From: kidscancode

You wrote pos.position, but pos is the mouse position, so it’s a Vector2. A Vector2 doesn’t have a position property, so that’s why you got an error.

I’t snot clear what you’re trying to do. You’re not spawning any box. If you wanted to do that, you’d need to call instance() on your box PackedScene. In that case, you probably want to do box.position = pos to set the box’s position to the mouse position.