How to make an object follow another object on a keypress?

Hi everyone! Im sorta new to coding outside of visual scripting methods, but I was wanting to make a short little test game in unity where I moved a block over on top of another block and hit the spacebar, and one would parent to the other so I could move it around, and then hit the space bar and drop it again. It shouldn’t be too complicated (I actually found a question before where someone asked this, but the code had bugs) but I can’t figure it out.I extremely appreciate your help!

Oh, just for specification, I can get the controllable cube moving all over, I just can’t parent. thx!

This is more the kind of thing you slowly figure out on your own like I just did instead of asking online, but here is your script. I hope you find it useful.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Move : MonoBehaviour {

	List<GameObject> children = new List<GameObject>();

	void Update () {
		//		Put your movement script here or in FixedUpdate
		if (Input.GetKey("p")){
			Collider[] nearObjects = Physics.OverlapSphere(transform.position, 25);//		5 meter check radius
			int i = nearObjects.Length;
			while(i > 0){
				if (nearObjects*.rigidbody != null){// if it has a rigidbody*

_ nearObjects*.gameObject.transform.parent = transform;_
_ children.Add (nearObjects.gameObject);
else if (nearObjects.gameObject.tag == “parentable”){// check through tag*

nearObjects*.gameObject.transform.parent = transform;
children.Add (nearObjects.gameObject);
if (Input.GetKey(“o”)){
for (int i = children.Count - 1; i > 0; i–){
children.gameObject.transform.parent = null;

If you want to map to other keys you can set them up in Edit>ProjectSettings>Input. Access them using (Input.GetButton (“Button name you used in the editor”))_