I have 2 scripts, one that shows text on the screen when a player enters a collider set as a trigger, and one that shows text when the user press a key, in this instance KeyCode.E.
Here are the scripts:
showTextOnTrigger.cs
using UnityEngine;
using System.Collections;
public class showTextOnTrigger : MonoBehaviour {
public GUISkin qSkin;
public string message;
private bool onTriggerState = false;
private bool onTriggerPressed = false;
void FixedUpdate() {
if(Input.GetKeyDown(KeyCode.E)) {
onTriggerPressed = true;
}
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player") {
onTriggerState = true;
}
}
void OnTriggerExit(Collider other){
onTriggerState = false;
}
void OnGUI() {
GUI.skin = qSkin;
if (onTriggerState !onTriggerPressed) {
GUILayout.BeginArea (new Rect((Screen.width/2)-160, (Screen.height/2-100) , 320, 600));
GUILayout.BeginVertical();
GUILayout.Label(message);
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
}
showTextOnUseKey.cs
using UnityEngine;
using System.Collections;
public class showTextOnUseKey : MonoBehaviour {
public GUISkin qSkin;
public string message;
private bool onUseKeyState = false;
private bool onUseKeyPressed = false;
void FixedUpdate() {
if(Input.GetKeyDown(KeyCode.E)) {
onUseKeyPressed = true;
}
}
void OnTriggerEnter(Collider other){
if (!onUseKeyPressed) {
if (other.gameObject.tag == "Player") {
onUseKeyState = true;
}
}
}
void OnTriggerExit(Collider other){
onUseKeyState = false;
}
void OnGUI() {
GUI.skin = qSkin;
if (onUseKeyState onUseKeyPressed) {
GUILayout.BeginArea (new Rect((Screen.width/2)-160, (Screen.height/2-100) , 320, 600));
GUILayout.BeginVertical();
GUILayout.Label(message);
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
}
The scripts does work somehow, but my problem is if i enter the collider set as trigger, the text does not show when pressing E at the other collider set as trigger, and vice versa.
I have no clue why this happens, anyone care to have a look.
You can use something like this to hide and show Gui Elements.
Also You can have a second variable for showGui, such as showGui2. Then after entering the trigger check if showGui showGui2 are true to display the text. Or only enable to user to use the e Key while showGui2 is true ( which means they need to be in the Trigger ).
var showGui : boolean;
function OnGUI () {
if(showGui) {
//showGui
}
}
function Update () {
if(Input.GetKeyDown("e")) {
//if showGui is true, then it will change it to false. If it is false, then it changes to true. Place anywhere you would like.
showGui = !showGui;
}
}
Did you see anything wrong with the conditional part of my script? If so please tell me.
What i cannot figure out is why when i trigger the one event, the other does not work.
Maybe it’s because you’re not using a tag for OnTriggerExit? I would use print(onUseKeyState); print(onUseKeyPressed);
So you can tell via the Console what the variables are changing to and when. It will help you solve the issue.
Also this may sound stupid but make sure in your inspector that you have some text written lol. It’s happened to me before. One other thing could be your text length, I’ve had an issue with long strings that don’t show up because the text exceeds the length.