- Home /
UI text glitch/bug in 2D game
I have a text. I enable/disable it. On some object it works fine. On another it doesn't show at all. Here is the script, maybe I'm doing something wrong idk.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextInfoPop : MonoBehaviour {
public Text text;
bool playerInZone;
// Use this for initialization
void Start () {
text.enabled = false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Space) && playerInZone){
text.enabled = !text.enabled;
}
else if(playerInZone == false){
text.enabled = false;
}
}
void OnTriggerEnter2D(Collider2D other){
if(other.name == "Player")
playerInZone = true;
}
void OnTriggerExit2D(Collider2D other){
if(other.name == "Player")
playerInZone = false;
}
}
Anny help is greatly appriciated, cause I'm gonna go crazy soon if i don't find out what's wrong.
make sure that all you items have a collider set to trigger, have you tried to build the game to see if the bug was still here?
Hi,
Your second object has a Collider2D and a Rigidbody2D too ?
As I can't saw that I cannot be sure of the problem but did you try your script without the playerInZone condition to be sure that the problem come from the collider.
I attach this script to a Vase(for example), and when player presses Space, UI text says "This is a vase".
The Vase GameObject doesn't need a rigidbody. The Player has Rigidbody2D and box + circle collider.
I tried attaching Rb2D to the Vase too.. Doesn't work. To be more specific, the text flickers, it shows for 0.001sec, and dissapears again. And when collide with another objects, it shows up just fine.
Built the game and ran it. Bug is still there.
Here are Images of the 2 gameobjects, 1 shows the texts when collided, and hides when player is not in zone, and the other shows for 0.01 sec and disappears. I must press 100 times Space, to read what the text says.
Your answer
![](https://koobas.hobune.stream/wayback/20220611232137im_/https://answers.unity.com/themes/thub/images/avi.jpg)