Hi there, i was testing somethings and i run into this problem,

i randomly call a scene(gem) on my main_scene. and i try to delete them when they interact with raycast raycast is on my player, and configired as collide with area's. (gem object has a area around it)

i have 2 scripts for it,

karakter.gd (player) triger.gd (gems)

this code is on my karakter.gd (player) script: there is also a group on my gems node, its name is "gems"

            	if $RayCast_gem_ball.is_colliding() and get_tree().get_nodes_in_group("gems"):
            		var removing = load("res://sahneler/triger.gd").new()
            		removing.remove_gem()

this code is on my triger.gd (gems) script

extends Spatial

var random_x = RandomNumberGenerator.new()
var random_y = RandomNumberGenerator.new()
var random_z = RandomNumberGenerator.new()


func _on_Button_pressed():
	for c in range(4):
		random_x.randomize()
		var random_x_number = random_x.randf_range(-20,20)
		random_y.randomize()
		var random_y_number = random_y.randf_range(1,1)
		random_z.randomize()
		var random_z_number = random_z.randf_range(-20,20)
		var gem_instance = preload ("res://sahneler/gem.tscn").instance()
		add_child(gem_instance)
		add_to_group("gems")
		gem_instance.set_translation(Vector3(random_x_number,random_y_number,random_z_number))
		

func remove_gem(interact_gem):
	interact_gem = get_child().queue_free()
	print (interact_gem)

i did try few things but its a no no in the end. im trying to learn so what is wrong with this code?

this is screenshoot

What are you trying to do in the remove_gem function? Are you trying to remove all gems, or just the gem that was interacted with and passed to the remove_gem function?

For removing all gems, you can use something like this (untested):

func remove_gem(interact_gem):
	for child in get_children():
		if child.is_in_group("gem"):
			child.queue_free()
	print ("All children gems are deleted")

For removing just the gem that is passed in, just interact_gem, you can use something like this (untested):

func remove_gem(interact_gem):
	print ("Removing :" + interact_gem.name)
	interact_gem.queue_free()
	print ("Gem removed!")
	interact_gem = null

Thank you for anwser, i m trying to remove just the gem that interact with the raycast ,but weirdly now script calling lines return with error, its one argument from function but dunno what should i write.

Try changing line 54 to the following:

removing.remove_gem($Raycast_gem_ball.get_collider())

Though, you probably want to get the trigger rather than load a new instance of the script at line 53. Regardless, the code above should fix the issue.

2 years later