2D Toolkit Forum

2D Toolkit => Support => Topic started by: rhart on April 27, 2012, 06:13:57 pm

Title: How do dynamicaly instantiate a tk2dTextMesh at runtime?
Post by: rhart on April 27, 2012, 06:13:57 pm
I have a need to dynamically add text into the scene at runtime. There may end up being multiple text meshes added to the scene by the user.

I have tried create a tk2dTextMesh in the editor and than dragging it into a prefab. Once I have the prefab, I tried to instantiate it and place it into the scene. For some reason it keeps telling me the prefab is null.

However when I manually drag the prefab into the scene, the default text shows up no problem. Any ideas I what I may be messing up or alternative approaches I could try would be greatly appreciated. Thanx
Title: Re: How do dynamicaly instantiate a tk2dTextMesh at runtime?
Post by: unikronsoftware on April 27, 2012, 06:46:32 pm
Just running through a basic use scenario here -

Code: [Select]
public GameObject prefab; // drag your prefab here

GameObject go = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
var textMesh = go.GetComponent<tk2dTextMesh>();
if (textMesh != null)
{
textMesh.text = "Prefab: " + Random.Range(0, 100).ToString();
textMesh.Commit();
}

That should work fine. If you were doing this or you're still having problems, let me know.
Title: Re: How do dynamicaly instantiate a tk2dTextMesh at runtime?
Post by: rhart on April 30, 2012, 07:18:15 pm
Yes, that worked. Thank You!