UI Text sometimes renders when instantiated, yet sometimes does not.
Every time I shoot an object in the game it destroys itself and the PopUpPairParent(Clone) is instantiated through a separate object and script called "FloatingTextManger" (see below). The PopUpPairParent(Clone) has a different component script "FloatingText" (see below) which takes in the animator component of PopUpPairParent(Clone)'s child, called PopUpPair. PopUpPair's child is PopUpTextC which is a UIText object. The animation moves the PopUpPair up and starts to decrease its alpha until 0 and at the end of the clip length PopUpPairParent(Clone) is destroyed.
This works perfectly ...... sometimes. A significant amount of the time the PopUpPairParent(Clone) is instantiated, PopUpPair animates with PopUpTextC but PopUpTextC's text never renders.
Here is some comparison screenshots of when it does and doesn't render:
Does Render:
Doesn't Render:
public class FloatingTextManager: MonoBehaviour {
public FloatingText popUPTextPairParent;
public GameObject canvas;
public void CreateFloatingText(Transform location) {
FloatingText instanceOfPopUPTextPair = Instantiate(popUPTextPairParent);
if (instanceOfPopUPTextPair) {
Debug.Log ("instanceOfPopUPTextPair exists");
}
Vector2 screenPosition = Camera.main.WorldToScreenPoint (location.position);
instanceOfPopUPTextPair.transform.SetParent(canvas.transform, false);
if (canvas) {
Debug.Log ("There is a canvas");
}
instanceOfPopUPTextPair.transform.position = new Vector2 (screenPosition.x, screenPosition.y + 80f);
Debug.Log ("CreateFloatingText Called");
}
}
public class FloatingText : MonoBehaviour {
public Animator floatingTextAnimator;
private Text floatingText;
void Start () {
AnimatorClipInfo[] clipInfo = floatingTextAnimator.GetCurrentAnimatorClipInfo (0);
Destroy (gameObject, clipInfo[0].clip.length);
Debug.Log ("clip length: " + clipInfo [0].clip.length.ToString ());
floatingText = floatingTextAnimator.GetComponentInChildren<Text> ();
if (floatingText) {
Debug.Log ("GOT TEXT COMPONENT FROM THE CHILD: " + floatingText.ToString());
}
}
}
PopUpPairParent(Clone) is the prefab that is being instantiated so they have the same layers and as you can see they are set to the same canvas, "WordAnimationsCanvas". Furthermore you can see from the Debug.Log() messages that the code seems to run in exactly the same way regardless of whether the text renders or not.
Lastly CreateFloatingText() is called in the DestroyObjects Script attached to the destructible objects and goes something like this:
public class DestroyableObjects : MonoBehaviour {
private FloatingTextManager floatingTextManager;
void Start () {
floatingTextManager = FindObjectOfType<FloatingTextManager> ();
}
void OnTriggerEnter2D (Collider2D collider) {
floatingTextManager.CreateFloatingText (gameObject.transform);
Destroy (gameObject);
}
}
At this point I have no idea why this is happening and any help would be appreciated. Thank you.
Edit: Oddly enough, if I run the game in an Android Studio emulator the problem is resolved and all the instantiated UIText is rendered.
have you checked on the component what's the cause? Is the alpha at 0 right from the start, or is the text just too long to fit into your Text field?
Ultimately the rendering and non-rendering text element are the same prefab so all the components should be the same. But yes I did check manually and both the rendering and the non-rendering texts starts with an alpha of 255. I have also adjusted the size of the text box, the font size, and the colour of the non-rendering text it just refuses to show up.
then maybe it's outside the camera's clipping plane if you use one?
Answer by Wylie-Modro · Dec 25, 2016 at 10:46 PM
If I run my game in iOS or Android emulator, or on a Android or iPhone the problem seems to be resolved.
Some friends and I have concluded that this is an annoying but relatively harmless bug inside the Unity Editor.