toggling transform.parent unresponsive

hello,

in the following code what i’m trying to do is when you press a key, and a certain object is colliding with the trigger, that object becomes child of the trigger. When you press that key again, the child is released. It appears to work fine, but it sometimes responds as it should and sometimes you press the key and nothing happens, and when the object is on the edge of the trigger it won’t be released anyomre.

function Update () {
}

function OnTriggerStay(other : Collider) {
	
	if (Input.GetButtonDown("pegar")  other.CompareTag("farol")){
				if(other.transform.parent != this.transform) {
					other.transform.parent = this.transform;			
				} else {
					other.transform.parent = null;
				}
		}
	}
}

OnTriggerStay runs at the same interval as FixedUpdate does, so it has the exact same problem with using GetButtonDown there. Namely, OnButtonDown only returns true during the single frame that it happens, and since FixedUpdate doesn’t run every frame, it will miss that event more or less frequently depending the Update/FixedUpdate ratio. So all Down input events should be checked every frame, such as in Update.

–Eric

thanks, I know that, in the real code the GetButtonDown is in the Update function, and returns a value to x : bool, wich is then used in the collision function, but in the end the result is similar to the one with the code like that

should I attach a scene exemplifying it?