Best way to clear transform object?

I want to delete the target once the user stops holding down the key. But null isnt working?
The issue is I cant destroy the Target because its the actual target picker itself. Just want it cleared from the slot until the user clicks again for a new target.
any other ideas?

public Transform target; // target to aim for
 
 private void Update()
                 {
                     if (Input.GetKey (KeyCode.Z)) {
                         if (target != null) {
                             agent.SetDestination (target.position);
                             character.Move (agent.desiredVelocity, false, false);
                         } 
                         else if (Input.GetKeyUp (KeyCode.Z)) {
                             target = null;
                             } 
                     }
                         else {
                             //character.Move(Vector3.zero, false, false);
                             Patrolling ();
                         }
                     }
 
 public void SetTarget(Transform target)
         {
         
             this.target = target;
         }

there is a target picker script if you wanna see that too.

        public float surfaceOffset = 1.5f;
        public GameObject setTargetOn;

        // Update is called once per frame
        private void Update()
        {
            if (!Input.GetMouseButtonDown(0))
            {
                return;
            }
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (!Physics.Raycast(ray, out hit))
            {
                return;
            }
            transform.position = hit.point + hit.normal*surfaceOffset;
            if (setTargetOn != null)
            {
                setTargetOn.SendMessage("SetTarget", transform);
            }
        }
    }
}

try this:

else if (Input.GetKeyUp (KeyCode.Z)) {
    Destroy (target);
}

I’m not sure I understood what you need, but your script looks weird. Let me fix the indentation first:

private void Update() {
    if (Input.GetKey (KeyCode.Z)) {
        if (target != null) {
            agent.SetDestination (target.position);
            character.Move (agent.desiredVelocity, false, false);
        } 
        else if (Input.GetKeyUp (KeyCode.Z)) {
            target = null;
        } 
    }
    else {
        character.Move(Vector3.zero, false, false);
    }
}

Note the:

if (target != null) {
    //something
} 
else if (/*some condition*/) {
    target = null;
}

Can you see the problem there? For the line “target = null;” code to be executed, the first if condition has to be false (the second if too, but makes no difference here), and it’ll be false only when target is already null. So, when target == null and the player realeased the Z, you set it to null.

Another strange thing is:

if (Input.GetKey (KeyCode.Z)) {
    //some code
    if (Input.GetKeyUp (KeyCode.Z)) {
        //something else
    }
}

If GetKeyUp returns true it means the Z key was released in that frame, so GetKey will always be false (GetKey tells you if the key is being pressed in that frame). So, the “something else” code will never be executed, GetKeyUp will never be true when GetKey is true, and the only frame GetKeyUp is true, GetKey is already false.

Maybe you want this, but as I said before, I’m not sure of what you’re trying to do.

private void Update() {
    if (Input.GetKey (KeyCode.Z)) {
        if (target != null) {
            agent.SetDestination (target.position);
            character.Move (agent.desiredVelocity, false, false);
        }
    } else if (Input.GetKeyUp (KeyCode.Z)) {
        target = null;
        character.Move(Vector3.zero, false, false);
    }
}

If you do this it should work:

Destroy(object.gameObject);

(Not tested, but should work)
Also, this is in JS, but it probably will be the same… IDK xD