Why GUIText Array is Not Displaying GUIText?

Hey everybody,

I have a GUIText array with random tips that I would like displayed using Random.Range.

The random tips works fine. However, the code I am using will only display a random tip if the other GUITexts are enabled in the Hierarchy.

Code:

var tipsArray : GUIText[];
var currentTip:GUIText;

function Start () {
	currentTip.enabled=false;
	showRandomTip();
}

function showRandomTip() {
	currentTip.enabled=true;
	currentTip = tipsArray[Random.Range(0,tipsArray.length)];
	currentTip.transform.position=Vector3(.5,.5,0);
	currentTip.fontSize=Screen.width/20;
	currentTip.enabled=true;
	print("SHOW currentTip");
}

This works fine, but it only works when all the GUITexts are enabled in the Hierarchy. If I disable them, so that you cannot see them when the scene is running, No GUITexts are displayed.

Which makes sense, because they are not enabled.

I thought that currentTip.enabled=true; would fix that, but it doesn’t.

Nothing shows.

Any idea what I’m doing wrong and how to fix this?

Thanks!

Your problem is the order of operations. You need to swap lines 10 and lines 11. You need to assign the new tip before you enable it. You are enabling the previous tip.

Assuming all the tips are at the same position, you could just as easily have only a single GUIText and set the text. Your tips would be an array of strings rather than an array of GUIText objects.