Can't set the position of an instantiated object

Godot Version

4.7.2

Question

I’ve been trying to create a simple script that generates a straight road that is split into sections, however I can’t figure out how to fix an issue I’m having.

var straightRoad = preload("res://Scenes/road.tscn")

func _ready() -> void:
	for road in range(100):
		var instance = straightRoad.instantiate()
		
		instance.position.y = position.y + road * 10 // ERROR HERE

The debugger is telling me that the instance I made does not have a position value, however the straightRoad resource is a Sprite2D, which should have a position value. The instance variable should be a Node2D, right?

Can you copy and paste the error message you get?
Also you need to add the instance as a child first before modifying it’s position value, try that.

Before you set the position, add the node as a child to whichever node you want the road to be a child to.

func ready() → void:
for road in range(100):
var instance = straightRoad.instantiate()
add
_child(instance)
instance.position.y = position.y + road * 10 // ERROR HERE