Need a little help with a pick up script

Hey guys , I need you to help me with my pick up script.

using UnityEngine; using System.Collections.Generic;

public class Keyring : MonoBehaviour { public AudioClip getKeySound; public float keyPickUpDistance = 5f;

private List _keys = new List();
 
public void AddKey(Key key)
{
_keys.Add(key);
 
if (getKeySound != null)
{
audio.PlayOneShot(getKeySound);
}
}
 
 
public bool HasKey(Key key)
{
return _keys.Contains(key);
}
 
 
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, keyPickUpDistance))
{
Key key = hit.transform.GetComponent();
if (key != null)
{
AddKey(key);
key.transform.parent = transform;
key.transform.localPosition = Vector3.zero;
key.gameObject.SetActiveRecursively(false);
}
}
}
}

}

This script is attached to my player.When I press the E button my player take the key and it work correct but I need to make a GUI appear(when I press E) and ask me if I want to pick it up or no.Can someone help me with that , because I dont actually know how does the gui on C works.
Thank you for your time.

First, if you don’t know anything about Unity GUI you should check out this: Unity - Manual: IMGUI Basics

Then what you could do is something like this:

private boolean displayGUI = false;

void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, keyPickUpDistance))
{
Key key = hit.transform.GetComponent();
if (key != null)
{
   displayGUI = true;
}
}
}

void OnGUI(){
  if(displayGUI){
    //display a window and stuff... to do that follow the link
        if(GUI.Button("Yes")){
          AddKey(key);
          key.transform.parent = transform;
          key.transform.localPosition = Vector3.zero;
          key.gameObject.SetActiveRecursively(false);
          displayGUI = false;
        }
        if(GUI.Button("No"))
          displayGUI = false;
  }
}

This is untested code so i don’t know whether or how well it will work in your case

Let me know if you run into problems.