Hello Guest

Author Topic: How do dynamicaly instantiate a tk2dTextMesh at runtime?  (Read 5562 times)

rhart

  • Newbie
  • *
  • Posts: 5
    • View Profile
How do dynamicaly instantiate a tk2dTextMesh at runtime?
« 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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How do dynamicaly instantiate a tk2dTextMesh at runtime?
« Reply #1 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.

rhart

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How do dynamicaly instantiate a tk2dTextMesh at runtime?
« Reply #2 on: April 30, 2012, 07:18:15 pm »
Yes, that worked. Thank You!