I have the following code which keeps giving nullreferenceexception. It is almost identical to the example given in the documentation. The TextMesh comes attached along with a script called "PlayerObject.cs" (script below) to a prefabricated GameObject. The GameObject is instantiated by Controller.cs (script below). Is there something special that needs to be done to get textmesh working if the gameobject you want a textmesh running on is instantiated from another script?
NullReferenceException: Object reference not set to an instance of an object
PlayerObject.Update () (at Assets/Scripts/PlayerObject.cs
PlayerObject.cs script:
using UnityEngine;
using System.Collections;
using ClassLibrary;
public class PlayerObject : MonoBehaviour {
public int playerID;
public string objectName;
public tk2dSpriteAnimator spriteAnimator;
public tk2dSprite sprite;
tk2dTextMesh textMesh;
void Start () {
textMesh = GetComponent<tk2dTextMesh>();
}
void Update ()
{
if(textMesh != null)
{
textMesh.text = "TEST";
//textMesh.text = objectName;
textMesh.Commit();
}
}
}
The above gameobject is instantiated from a separate controller script, Controller.cs:
GameObject po;
po = (GameObject)Instantiate(Character,vector,Quaternion.identity);
PlayerObject poScript = (PlayerObject)po.gameObject.GetComponent(typeof(PlayerObject));
po.name = goList[i].name;
poScript.playerID = goList[i].pId;
poScript.objectName = goList[i].name;
playersConnectedGO.Add (po);