Can anyone explain this strange result in for loop?

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

i was trying to apply actions on specific elements of a group named “object”, i have two rigidbodies in the group, i used for loop to iterate through group elements nd if distance between element and player is less than 4 actions will be applied, strangely not all actions gets applied, here is the code

extends Spatial
var axis
var kin
func _ready():
	var rig1=$rig1
	var rig2=$rig2

func _physics_process(delta):
	for elem in axis:
		if (kin.translation.distance_to(elem.translation)<4):
			if Input.is_action_just_pressed("ui_accept"):

the visible property only changes with the last rigid body in the group, when i’m near the first element the button doesn’t appear, what confuses me is that the nested if works with both of them, when i’m near first element it collects it nd when i’m near second element it collects it as well, so the code is calculating the distance between each one of them nd the player but i don’t get why the button icon doesn’t become visible with both, it only becomes visible when distance between last element of group object nd the player is less than 4, does anyone know why that might be?

Aren’t you shutting off the button in the else?
For example if axis[0] < 4 == true the button gets turned on (visible).
But then the loop continues and the next element axis[1] > 4 shuts the button off.
Conversely if the axis[0] > 4 the button gets shut off and axis[1] < 4 the button gets turned back on.
This would make it seems as though only the last elem is working properly.

LeslieS | 2023-01-28 02:24