Help with getting ref to another object and executing a function

Godot Version

Hi, I have a trap scene that has the function _BuildTrap() in its attached script.
In the player character, I have this snippets of code to trigger it, but when I play and try to trigger it, I get the error “Invalid call. Nonexistent function ‘_BuildTrap’ in base ‘Nil’.” which I guess means that var trap doesn’t get the right scene and can’t find the function to trigger. Could someone help me with it? I am sorry if this is a stupid question but I am new to programming :slight_smile:
Thanks in advance

var trap = null
if event.is_action_pressed("interact") and CollidingWithTrap == true :
func _on_area_3d__player_collision_detector_area_entered(area):
	if area.is_in_group("Trap"):
		var trap = get_tree().get_first_node_in_group("Trap")
		CollidingWithTrap = true

make the logic exactly the opposite, the trap will expect a collision of the player to enter it, so in the trap you should test area.is_in_group("Player")

1 Like

Initially that’s how it was set up, but I found that if I placed down more than 1 trap, all of them would trigger, hence why I am assigning the value of trap dynamically inside the player’s controller script

but that means you’ve done something wrong, because it shouldn’t work that way. if a player collides with one collision, the other collisions can’t collide

is CollidingWithTrap a global variable? if you had it like this in the previous version, this could be a problem, it could trigger all traps

but this is conceptually correct, because imagine that you will add more and more objects to the game and you will control it with the player… it’s not very ideal

1 Like

you are absolutely right, I tried what you suggested and fixed some other error and now it works perfectly, thank you! :trophy:

1 Like

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