- Home /
instantiate 3d text with different names - c#
Hi,
I have a List of strings all with a different name.
For each string I'm instantiating 3dtext that needs to take each List entry's name as it's own.
I have it working for the most part, but my 3dtext will only take the name of the last. I'm nearly positive it has something to do that my code gives the same name to each instance of the 3dtext, but have no clue how to solve this. itemsTwo = my list. it will constantly have a different amount of objects with different names in them.
foreach (string s in itemsTwo)
{
Vector3 positionItemButton = new Vector3(0, posY, 0);
Vector3 rotationItemButton = new Vector3(90, 180, 0);
Vector3 rotationText = new Vector3(180, 180, 180);
Instantiate(button, positionItemButton, Quaternion.Euler(rotationItemButton));
//set public text to List contents, so prefab can use it.
itemName = s;
//instantiate prefab
Instantiate(buttonText, positionItemButton, Quaternion.Euler(rotationText));
posY = posY - 2.0f;
}
and the prefab has this as code (in Start):
if (name != null)
{
name = itemLocation.Instance.itemName;
(gameObject.GetComponent(typeof(TextMesh)) as TextMesh).text = name;
}
so right now I am getting X number of 3dtext all with the same name. any ideas on how to reach each string seperately?
that is because the variable name is same for all the instances....
or are u changing the name somewhere??
Answer by whydoidoit · Jun 14, 2012 at 11:19 AM
Well either actually rename in Awake() - so it actually happens during the Instantiate call:
Or even better - create a generic list of them:
public static List<TextMesh> texts = new List<TextMesh>();
....
var x = Instantiate(buttonText, positionItemButton, Quaternion.Euler(rotationText)) as WhateverButtonTextPrefabIsDefinedAs;
texts.Add(x.GetComponent<TextMesh>());
Now you can access them using the array:
YourScriptName.texts[2].text = "blah";
Oh yeah - probably don't check for the name being null too!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instatiating a prefab in a random position 0 Answers
Issue Instantiating prefab in C# 0 Answers
Missing (Game Object) 3 Answers