- Home /
Help with chatting to NPC, click F for gui box etc
Hi I'm making a script where the player would input F on his keyboard to 'speak' with an NPC. What will really happen, is upon inputting that key, the gui box and label would pop up. Here is what I've got.
using UnityEngine;
using System.Collections;
public class Chat : MonoBehaviour {
public GameObject target;
public Texture2D icon;
public Texture2D icon2;
public void OnGUI(){
var talk = false;
if(Input.GetKeyUp(KeyCode.F)) {
talk = true;
}
if (talk == true){
GUI.Label (new Rect (Screen.width/2-290,Screen.height/1-100-70,150,100), new GUIContent(icon));
GUI.Label (new Rect (Screen.width/2+245,Screen.height/1-100-70,150,100), new GUIContent(icon2));
GUI.Box (new Rect (Screen.width/2-290, Screen.height/1-100, 600, 80), "\nWelcome Kid");
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
the problem is, by clicking on F it does nothing. Further testing shows without the key input, for example, using an If(y>x) then talk=true, works. How ever that method would obviously not work as it means either the text is off or on when the game starts.
This is code is aimed to be simple as once I've passed this step I can move on to checking things like, if the target NPC is close or not, closing the text after the player is done etc. I have tried to search up but the codes that resulted from previous similar questions were of the javascript format and I'm very unfamiliar with that.
Any help is much appreciated.
Hmmm looks ok to me, have you tried putting your input code in the update function. Or using an on screen gui button ins$$anonymous$$d of a key press.
Answer by Potumba · Feb 20, 2013 at 10:51 PM
yeah probably you will have to put the if statment that is checking if key F is pressed into the Update function and it should work :)