Textobject.text not displaying the value it's set to on first update but is on second and subsiquent

You’ll have to bare with me as I’m relatively new to unity.

I have a prefab of a button with a few nested children, including a text object and an image object among other things.

Within the button prefab I’ve attached a script with properties for each of the child objects and attached them within the studio.

I have a GameControler object that on start() populates a scrolling list of buttons on screen.

The buttons are created from the prefab, and within start() of the GameControler after each button is instantiated and given a unique name I save the new button object to a property.

Then within update() of the GameControler I use SendMessage to call a procedure in the script of the button prefab which sets the value of the text object to whatever value I require.

This all works fine, but not on the first update.

On the first update the text object still displays its initial value “some text” rather than the new value and I can see that the procedure in the script of the button prefab is called and is setting it to the correct value.

Is this likely to be a timing issue, in that the text object isn’t yet instantiated on the first update??

Thanks

seeing the actual code is much better than describing it to us… the interpretation you are applying can be biased since you know what it’s supposed to do, when the fault is in what it is actually doing.

It might be a timing issue as you suggested. I’m not sure exactly what you’re trying to do, but I think I know what should work. Assuming you’re using the standard button as the prefab, it might look something like this.

void MakeButton()
{
//Instantiate the Button prefab
GameObject newButton = Instantiate(buttonPrefab, desiredPos, desiredRot) as GameObject;

//Find and set the text part of the button
newButton.GetComponentInChildren<Text>().text = "Desired Text";
}

Thanks for the reply’s, Of course you’re right, seeing the actually code would help, I’ll edit my post as soon as I get into the office.

I would also caution against sendmessage. I believe it’s slow from what I remember. I can’t recall, but I think it’s slower than getComponent. I could be wrong, but just a possible note!

1 Like