Chat bubble for NPC when player is close

This Might be a fairly simple solution to this but a lot of the answers in finding are either for 3d or take a large route and add way to many things to it. I am simply trying to make it so that when a player gets close to the target/NPC a small chat bubble would appear above their head when go away when they leave.
This is a very simple version of what I have at the moment. There was a line that called for AddComponent() but it kept telling me the command simply doesnt exsist. Even when I look it up on the unity wiki and what not.

    void FixedUpdate()
    {
        var ChatBubbleShowing = false;

        float minDist = 3;
        float dist = Vector3.Distance(pl.transform.position, transform.position);

        if((dist < minDist) && ChatBubbleShowing == false)
        {
            ChatBubbleShowing = true;
            var chatBubble = new GameObject("Chat Bubble");
        }    
    }

=======

The main goal Im having at the moment is try trying to make it so that the player walks up to the NPC (2d top down), a small text box shows up over the NPC showing that you can interact with the NPC. and when the player does. They movement is locked, and a GUI shows up. A player can then close the GUI and then once again move again. instead of locking their movement. Simply walking too far will also cause the GUI to close. But again I am a bit stuck on the the whole “Creating and Destorying a game object” part.

I assume that this piece of code is on the NPC itself. In order to destroy the gameobject text after you create it, make sure that the scope of the var chatBubble is availble to destroy it. I’d just create the var chatBubble at the very beginning and then set it to your new GameObject.

var chatBubble     
float minDist = 3;
var ChatBubbleShowing = false;
float dist;

void FixedUpdate()
     {
         dist = Vector3.Distance(pl.transform.position, transform.position);
 
         if((dist < minDist) && ChatBubbleShowing == false)
         {
             ChatBubbleShowing = true;
             chatBubble = new GameObject("Chat Bubble");
         }    

         if ((dist > minDist) && ChatBubbleShowing)) 
         {
             ChatBubbleShowing = false;
             Destroy(chatBubble);
          }
     }

AddComponent works fine for me, just make sure you use <> instead of ()

chatBubble.AddComponent<RigidBody>();

Another thing, I’d recommend that you just create the text gameobject itself first and attach it to the NPC. Then reference the text gameobject with a public variable and just use SetActive to turn it on and off. This helps with avoiding too much instantiation and deleting (though it’s not really a big concern right now) and it’s easier to reference to text gameobject to change the text and avoids having to add components through code which can get repetitive. We could talk on discord if you want, it’d be a lot faster. Hope this helps.