Hi, all-- I have a project with two scenes, a ‘title screen’ and a game level. When the project is first loaded and run, the title screen appears along with text that says ‘click anywhere to continue’. After you click, the game level scene is loaded. When you press ‘Escape’, you are taken back to the title screen scene, but the text is no longer visible. When the ‘game mode’ is exited, you can’t see it in the Unity3D editor, either, and it doesn’t reappear when the project is run again.
I’ve found the cause, but don’t know how to fix it; I have two canvases with text controls ‘on’ them in one scene, and one canvas with a text control on it in the title scene. I have a ‘fade out’ script that is only applied to one text object, yet the script is fading out ALL text on every canvas in every scene. Clearly there is something I don’t quite understand here.
When the script is disabled the text controls don’t fade out, of course, and the controls are visible in each scene as I switch between them while in ‘game mode’.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FadeText : MonoBehaviour {
private Text txtMsgText;
// Use this for initialization
void Start () {
txtMsgText = GetComponent<Text> ();
Color c = txtMsgText.material.color;
c.a = 1.0F;
txtMsgText.material.color = c;
}
// Update is called once per frame
void Update () {
if (txtMsgText.material.color.a > 0.0F) {
Color c = txtMsgText.material.color;
c.a -= .01F;
txtMsgText.material.color = c;
}
}
}
This script is ONLY attached to the single text control in ONE scene that I want to fade out-- so really perplexed as to why it’s fading out every text control in every scene…
Ah, that’s because you’re not fading out the text; you’re changing the color of the material which is shared among all the texts. (And material changes happen in the project, not in the scene, which is why it persists even after you quit the run.)
A much easier (and actually worky) way of doing this is to use Graphic.CrossFadeAlpha. Sadly the documentation doesn’t appear to contain an example, but it’s really easy to use. If you want a script that fades its own graphic (which could be a Text or any other Graphic) out on start, as your script is doing now, then just do:
using UnityEngine;
using UnityEngine.UI;
public class FadeOut : MonoBehaviour {
public float fadeTime = 1f;
void Start() {
Graphic g = GetComponent<Graphic>(); // Text, Button, Image, etc.
g.CrossFadeAlpha(0, fadeTime, true);
}
}