- Home /
Simple text box, directions?
In the 3D game i am currently working on, (First person), i want the player to walk up to an NPC ( i have them out in the map) press a button, and have a text box appear, with text. Then another press of the button and the text box is gone.
I am wondering if anyone could point me in directions for where i could get a tutorial on this specific thing, (Or advice when making the scripts for this to happen.)
Thank you.
Answer by ElijahShadbolt · Dec 15, 2017 at 08:53 PM
You can use the Unity UI system and EventSystems coupled with some custom scripting to create this logic.
GameObject > UI > Text - this will create a new Text element (along with a UI Canvas and EventSystem) and you can edit the displayed text (and change it dynamically with its text
property).
Position UI elements with the RectTransform tool found in the Toolbar. In the Scene view, drag the Text element inside the (very large) Canvas rect so you can see it in Play mode.
Create a custom script (C#) - in the Insepctor > Add Component > New Script - give it a name, e.g. MessageBox. Then click the new component's gear icon > Edit
In the new MonoBehaviour class add the following code:
public UnityEngine.UI.Text textbox;
void Update()
{
// if player presses 'E' key this frame
if (Input.GetKeyDown(KeyCode.E))
{
// if textbox reference exists
if (textbox != null)
{
// toggle textbox (visible/hidden)
bool wasActive = textbox.gameObject.activeSelf;
textbox.gameObject.SetActive(!wasActive);
}
}
}
In the Inspector of the MessageBox component, assign a reference to the Text element by dragging it from the hierarchy into the Textbox
property of the MessageBox component.
For more information, Google is your friend.
Answer by Daniel-Talis · Dec 12, 2017 at 06:30 AM
You may need either a 'dialogue system' or a 'notification system'.