- Home /
Issue displaying text on screen
I have 2 characters in my scene, each using the same script. I would like each one to have their own dialogue which I specify in the inspector. I would like each character to display that text when I get close enough to them. This works fine in the console when I use Debug.Log, the console prints the correct message when I'm close enough to the characters.
The problem is, it is only displaying the message on screen for one character but not the other, like so:
As you can see, when I get close to the red character, the text and his camera close up is displayed, but with the yellow character, only the camera close up is displayed. What am I missing?
I made the red character and then just copied it to make the yellow on so each character has the same attachments in the inspector.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class showText : MonoBehaviour
{
public GameObject player;
public Button input;
public Text NPCtext;
public Camera cam;
public string inputField;
private int size;
public float distance;
void Start()
{
}
void Update ()
{
distance = Vector3.Distance(player.transform.position, transform.position);
Debug.Log(distance);
if(distance < 5)
{
NPCtext.text = inputField;
input.active = true;
cam.active = true;
}
else
{
NPCtext.text = "";
input.active = false;
cam.active = false;
}
}
}
Have you set all of the variables (player, input, NPCText, cam, ect) in the inspector? Or maybe you placed the wrong variable (in the inspector). $$anonymous$$ake sure the variables are right.
Can we see screenshots of both of the scripts in the inspector?
Yeah they're all set, I'm certain everything is right. Here's both inspectors:
yellow:
red:
As I was saying, in the console, everything works fine, yellow and red characters display the corresponding text, just getting it to display on screen is the issue
I just realised what is wrong. On the red character, it is disabling the object. Get rid of the line that disables the text, and see if the text changes. It is probably not working, as it is disabled as you walk out of the zone, and you can't re enable it.
Do you disable the text object in any script? To debug the code, comment out that line, and see if the text changes.
If it does, you need to broadcast that the text needs to be displayed (to the other objects). If it doesn't, I'm not sure whats wrong.
Answer by SkaredCreations · Dec 17, 2014 at 12:35 AM
At line 38 you're resetting NPCtext.text to empty string, so while the closer is setting to inputField at the same time the other is resetting it. You should use a trigger collider that gives you also more performance than checking the distance at each frame in more scripts (OnTriggerEnter will the the text, OnTriggerExit will reset to empty)
I see, this makes sense now. Thanks very much, this is working now. @Lucas$$anonymous$$ars, i understand now what you were saying about disabling the text, I would like to thank you also, you guys have been great
Your answer
![](https://koobas.hobune.stream/wayback/20220613174216im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make GUI Text appear after a certain amount of time 2 Answers
what is the correct way to display text on screen 1 Answer
Trouble displaying lists of strings in a text box 1 Answer
Display a text for all scenes 2 Answers
UI Text not displaying for some game objects when Player is within distance of the object 0 Answers