Hi, I have identified my problem and looking for a workaround, if anyone knows. What happens is that as soon as I set the text value of a world space text, the text disappears. The value is there properly and can be seen in the editor, but is invisible in the game view.
I have a world space canvas that constantly looks at the camera, that contains some game bars and a title that pulses (scales up and down constantly). It works great as an announcement board for my game. Now I wanted to add some simple floating texts for things like + score, + health etc.
I am using a text prefab with a script that has a coroutine for making the text move up the Y axis slowly. This is the coroutine that shows that I first change the text, then start the coroutine for making the text fly upwards.
private IEnumerator ShowAssistObjectResults(float energy)
{
floatingTexts[0].SetText("Energy",energy);
floatingTexts[0].PlayFloatText();
yield return new WaitForSeconds(breakTime);
}
This is my SetText method:
public void SetText(string text, float value)
{
floatingText.text = value > 0 ? text+ " + " + value : text + myValue;
floatingText.color = value > 0 ? positiveColor : negativeColor;
}
And finally the FloatText Coroutine:
public void PlayFloatText()
{
StartCoroutine(FloatAndDisable());
}
IEnumerator FloatAndDisable()
{
t = 0f;
while (t < flyDuration)
{
t += Time.deltaTime;
y = _rectTransform.anchoredPosition3D.y;
_rectTransform.anchoredPosition3D = new Vector3(x,y + floatingSpeed*t ,z);
yield return null;
}
_rectTransform.anchoredPosition3D = startPosition;
floatingText.text = "";
}
I have tried setting the text to random values in the code (e.g. “test” ) and the floating text works as intended. For some specific reason, using the one line ifs I have above or passing another value makes the text go invisible. I am using Unity 2018.3.0f2.
I suspect it has something to do with setting the text to be “dirty” even though I am not really familiar with the definition of that.