Hello Guest

Author Topic: tk2dTextMesh at runtime  (Read 4278 times)

prodyminds

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
tk2dTextMesh at runtime
« on: September 03, 2013, 02:16:08 am »
I'm trying to create a tk2dTextMesh at runtime.

I have my class Title

Code: [Select]
public Texture icon;
public string text;
public bool showingContent;

public List<Title> parent = new List<Title>();
public List<Title> menuList = new List<Title>();
public List<Content> contentList = new List<Content>();

And my function:

Code: [Select]
void CreateTitle(Title title){

GameObject parent = new GameObject();
GameObject icon = new GameObject();
GameObject back = new GameObject();
GameObject text = new GameObject();

menuManager.gameObjecsInScene.Add(parent);

icon.transform.parent = parent.transform;
text.transform.parent = parent.transform;
back.transform.parent = parent.transform;
parent.transform.parent = menuManager.tk2DScrollableAreaContent.transform;

icon.name = "Icon";
text.name = "Title";
back.name = "Back";
parent.name = title.text + " title";

parent.transform.localPosition = Vector3.zero;
back.transform.localPosition = Vector3.zero;
icon.transform.localPosition = new Vector3(-16, 0, 0);
text.transform.localPosition = new Vector3(-12, 0, 0);

tk2dTextMesh textMesh = text.AddComponent<tk2dTextMesh>();
textMesh.anchor = TextAnchor.MiddleLeft;
textMesh.text = title.text;
textMesh.maxChars = 50;
textMesh.scale = Vector3.one * 15;
textMesh.Commit();

}

But i always get:

Quote
NullReferenceException: Object reference not set to an instance of an object
tk2dTextMesh.Commit () (at Assets/TK2DROOT/tk2d/Code/Fonts/tk2dTextMesh.cs:540)
MenuManagerEditor.CreateTitle (.Title title) (at Assets/Editor/MenuManagerEditor.cs:143)
MenuManagerEditor.CommitMenu () (at Assets/Editor/MenuManagerEditor.cs:110)
MenuManagerEditor.OnInspectorGUI () (at Assets/Editor/MenuManagerEditor.cs:34)
UnityEditor.InspectorWindow.DrawEditors (Boolean isRepaintEvent, UnityEditor.Editor[] editors, Boolean eyeDropperDirty) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Editor/Mono/Inspector/InspectorWindow.cs:888)
UnityEditor.DockArea:OnGUI()

In inspector, all the values i set are perfectly in place, but the text doesn't apear. If a change in editor the text, then it create the mesh and the text apear.

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: tk2dTextMesh at runtime
« Reply #1 on: September 03, 2013, 09:30:18 am »
It looks like you've missed assigning a font e.g. textMesh.font = ...

You can just create a public tk2dFont object and assign it in the editor.

prodyminds

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: tk2dTextMesh at runtime
« Reply #2 on: September 03, 2013, 11:13:10 am »
Yes, i don't set it ny code, but in editor appear. I'm gonna try this