How to Implement a pop up text box window

Hey guys,
First of all, sorry, this is my second post today seeking advice (on a different subject). I find this forum an incredible source for help and I really do appreciate it.

So what I would love to do is implement a sort of pop up text box during game play, at the moment I have no idea how to do it. I’ve googled around and I can’t find anything helpful, thus the reason for posting here.

What I want it to do is:

  • The player walks up to an item:
  • When within range, information about the item will pop up on the UI.

Example information in pop up box:

  • Sunglasses (item name)
  • 200g (weight)
  • Used on bright days (item description)

I’d assume we would utilise a box collider to gauge distance from the object and the GUI to display the information about the object.

Any help is appreciated!

Still stuck on this problem :frowning:

You’re pretty much on the right track. If you haven’t, definitely check out the tutorials on Unity UI.

This is, as you’ve figured out, a problem with two parts: the dialog box is one, and the in-world item’s info is the other. Make a script for each; let’s call it DialogBox and PickupItem. You may want to create a class to hold information about the item itself (the name, weight, description; probably some other information will be added later). This class can be tacked onto the end of the PickupItem script:

[System.Serializable]
public class ItemInformation {
public string name;
public int weight;
public string description;
}

Go ahead and future-proof this class; add in fields for any data you’re going to find useful about items like this, not just this one dialog box. Selling price? Maybe a link to a Sprite for an icon? Anything that’ll be useful in your game.
Now, in the main body of PickupItem, you’ll reference this class:

public ItemInformation thisItem;

Now, check the inspector for the pickup. You’ll be able to edit all this information there. (That’s what System.Serializable does, by the way)

At this point, you’ll want to attach the pickup to the box collider. (Make sure it’s a trigger collider.) Now, in order to get information to pop up, you’ll have to implement OnTriggerEnter and OnTriggerExit. Now, what exactly to do with OnTriggerEnter is a matter of some decision-making, depending on how you want to structure your code.

Whenever possible, I like to make in-game objects completely unaware that a UI exists; the UI does all the work using publicly exposed properties. I’ll tell you upfront that this task, on its own, is a lot simpler if you don’t do that - the item can directly tell the dialog box “Hey, here’s a thing, show it on the screen!” and be done with it. The drawback to this quick fix is that, later, it’ll be more complicated to make changes to your code. The best way to implement this for long-haul durability would be to have the UI (as a whole) contain a link to one “Player” or Character object; the Character class has a “public PickupItem viewingPickupItem;”; and the PickupItem sets that reference to itself when the player comes in range (and sets that to null when the player leaves its range). From there, the UI picks up on that change, and displays the dialog box.

At this point, I’ll let this be a choose your own adventure kinda thing. If you’d rather do the quick fix and need help with that, let me know; otherwise, see if you can work through the suggestions in the above paragraph and let me know if you need help with any parts of them.

Ok, firstly thanks for the hugely informative/descriptive reply! :slight_smile:
Some points in the post I don’t understand :smile:

However the effort I’ve made so far (doubt it’s the most efficient way, but it’s easy for me to understand) is…

ItemInformation script is attached to the book game object
I filled in the value fields to be exactly what I wanted for the book object.

ItemInformation script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[System.Serializable]

public class ItemInformation : MonoBehaviour
{
    public string itemname;
    public float weight;
    public string description;

    void Start () {
    }
   
    void Update () {
    }
}

Now I have:

public ItemInformation thisItem;

at the top of my ObjectPickup script.

In my OnTriggerStay function in my ObjectPickup script I’m trying to perform:

Debug.Log(ItemInformation.itemname,ItemInformation.weight,ItemInformation.description);

However I get this error:

Appreciate your help! :slight_smile:

EDIT:

OOPS! Think I made a mistake, I tried this too! :slight_smile:

Debug.Log(thisItem.itemname,thisItem.weight,thisItem.description);

and got these errors:

Checked out this yet?

Are you trying to format a string with that information?

Debug.Log(string.Format("{0} {1} {2}", thisItem.itemname,thisItem.weight,thisItem.description) );