x


setting the text of an instantiated prefab's child's guitext object

Hi - i'm instantiating a prefab at runtime, and trying to set the gui text of a nested gameobject, but my syntax isn't working. This is my code:

var scoreBoard = Instantiate(scoreBoard, Vector3 (0, 0, 0), Quaternion.identity);
var yourScore = transform.Find("YourScore"); //YourScore is a child of scoreBoard.
yourScore.GetComponent(GUIText).text = userScore; //yourScore has a GUIText attached to it, and userScore is a stored String.

how do I do this properly?

more ▼

asked Jul 28 '11 at 09:36 PM

heaversm gravatar image

heaversm
90 30 39 39

It is a bit odd that the variable you are Instantiating is the same name as the one you're then assigning to. I presume there is a class member called scoreBoard that you are dragging a prefab to.

Jul 28 '11 at 10:13 PM Waz
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Seems you mean:

var yourScore = scoreBoard.transform.Find("YourScore"); //YourScore is a child of scoreBoard
more ▼

answered Jul 28 '11 at 10:11 PM

Waz gravatar image

Waz
6.5k 23 33 71

thanks - that worked. Revised code as follows:

var scoreBoard = Instantiate(scoreBoard, Vector3 (0, 0, 0), Quaternion.identity); var playerScore = scoreBoard.transform.Find("YourScore"); playerScore.guiText.text = scoreTextRef.text;

Jul 29 '11 at 03:07 PM heaversm

While this is correct, it's bad form to use the same name for your new local variable as the name of the member variable, so either name the member to eg. scoreBoardPrefab or the new local variable to eg. scoreBoardInstance.

Jul 29 '11 at 05:27 PM Waz

Gotcha, thanks.

Jul 29 '11 at 06:00 PM heaversm
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1725
x1285
x848
x319

asked: Jul 28 '11 at 09:36 PM

Seen: 899 times

Last Updated: Jul 29 '11 at 06:00 PM