Why my text is overwritten on previous one?
Asked Answered
R

2

0

In my project there is a Main Camera and a Text UI on a Canvas and the below script.

84818-image1.png

Why is my text get superpose on the previous text after pressing the spacebar and how can I avoid this behaviour?

using UnityEngine;
using UnityEngine.UI;

public class PlayBoard : MonoBehaviour
{
    static Text message; 
 
    void Awake()
    {
        message = GameObject.Find("messageBox").GetComponent<Text>();
    }

    void Start()
    { 
        message.text = "To begin press space bar";
    }

    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            message.text = "New Message should appear in clear";
        }
    }
}
Ridenhour answered 3/6, 2024 at 17:1 Comment(0)
R
0

Finally I had to put the Unity Project to the recycle bin and start a new one using exactly the same script. This time the text field does act normally. I assumed that something was corrupted in the many lines of code Unity add to make a project work.

Ridenhour answered 28/12, 2016 at 4:44 Comment(2)

The camera has clear flags. If this is set to "Don't clear", the things that you drew will not be cleared when rendering the next frame. You probably played around with it and ended up with the value "Don't clear". See here: https://docs.unity3d.com/ScriptReference/CameraClearFlags.html or here: https://docs.unity3d.com/Manual/class-Camera.html

Batangas

Yes, thanks ScaniX, this is the right answer. The Clear Flags was set at Depth only. If I put it at either Skybox or Solid Color, everything works nicely. Bottom line don't mess-up with settings if you don't know what you are doing...

Ridenhour
W
0

Ok, had this happening for awhile and finally figured it out. The Camera was only covering like 90% of my Game Window area. The place where the UI text was, was actually out of the Camera frame. Since the background was black I didn’t notice the difference. Any UI outside of the Camera frame acts funky. As soon as I moved it into the camera frame it worked perfectly.

Wharton answered 3/6, 2024 at 16:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.