.instance() error

Godot Version

4.1.1

Question

I’m new to Godot and GDScript, and was trying to instance a different scene so I can get a tilemap from it, but it returns an error.

Here is my code:

extends Area2D

var rotating
var pickcollision
@onready var limb = $Beardy/Area2D/Pickaxe

func _ready():
	pickcollision = $Beardy/Area2D
	
	# Load the scene containing the TileMap
	var scene = load("res://Scenes/Themine.tscn")
	
	# Instance the scene to access its nodes
	var scene_instance = scene.instance()
	
	# Find the TileMap node within the scene
	var tilemap = find_tilemap_node(scene_instance)
	
	pickcollision.connect("tilemap_entered", self, "_on_tilemap_entered")
	# Connect the body_exited signal to the _on_body_exited method
	pickcollision.connect("tilemap_exited", self, "_on_tilemap_exited")

func find_tilemap_node(node):
	# Recursive function to find the TileMap node within the scene
	if node is TileMap:
		return node

func _on_body_entered(tilemap):
	# Called when the CollisionShape2D enters another physics body
	print("Collision entered with", tilemap.name)
	rotating = false
	pickcollision.set_disabled(false)
	limb.visible = true

func _on_body_exited(tilemap):
	# Called when the CollisionShape2D exits another physics body
	print("Collision exited with", tilemap.name)
	rotating = true
	pickcollision.set_disabled(false)
	limb.visible = true;

And it returned the error:

Invalid call. Nonexistent function 'instance' in base 'PackedScene'.

Use instantiate() instead of instance()

You’ve just used the wrong function name. What you’re after is instantiate(). I believe instance() is Unity’s version.

Thanks! Now I have to work out another issue with .connect() :wink:

1 Like

Thanks!

1 Like

What’s wrong with connect?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.