Hello Guest

Author Topic: Script for every TextMesh?  (Read 4190 times)

maaboo

  • Newbie
  • *
  • Posts: 1
    • View Profile
Script for every TextMesh?
« on: January 07, 2013, 11:03:43 am »
Hi!

I'm choosing a 2D framework and 2D Toolkit looks great. I have one question at the moment: Do i really need to create a separate script for every text mesh on the scene? As i can see in the documentation ? yes, i do. It looks unconvinient (may be i'm wrong). Is it possible to insert code in another game objects? For example:

Code: [Select]
public class Player : MonoBehaviour
{

public tk2dTextMesh labelOne;
public tk2dTextMesh labelTwo;
public tk2dTextMesh labelThree;

...

}

 void Start()
    {

labelOne = GetComponent<tk2dTextMesh>();
labelTwo = GetComponent<tk2dTextMesh>();
labelThree = GetComponent<tk2dTextMesh>();

}

void Update()
    {
ShowText();
   }
   
void ShowText()
    {
labelOne.text = labelOneValue.ToString();
  labelOne.Commit();
labelTwo.text = labelTwoValue.ToString();
  labelTwo.Commit();
labelThree.text = labelThreeValue.ToString();
  labelThree.Commit();
    }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Script for every TextMesh?
« Reply #1 on: January 07, 2013, 11:17:30 am »
Your code is getting the same textmesh on the same object... What you want to do is drag the appropriate textmeshes into the correct slots in the inspector, and get rid of the GetComponent<...> calls. That way Unity has a direct reference to the object, and you can call & commit whenever.

This is the most efficient way to do it in Unity as it doesn't involve any name lookups, etc.