Hello Guest

Author Topic: NullReferenceException when using tk2dTextMesh  (Read 5814 times)

Euthyphro

  • Newbie
  • *
  • Posts: 20
    • View Profile
NullReferenceException when using tk2dTextMesh
« on: September 13, 2013, 05:54:47 am »
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?

Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
PlayerObject.Update () (at Assets/Scripts/PlayerObject.cs

PlayerObject.cs script:
Code: [Select]
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:
Code: [Select]
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);
« Last Edit: September 13, 2013, 06:32:39 am by Euthyphro »

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: NullReferenceException when using tk2dTextMesh
« Reply #1 on: September 13, 2013, 08:54:12 am »
Hi,

Does the Character object you are using for instantiating have a tk2dTextMesh component on it?

Euthyphro

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: NullReferenceException when using tk2dTextMesh
« Reply #2 on: September 13, 2013, 09:23:04 am »
Yes it does. Right now I have text above the object that just says "Hey", I was hoping to be able to control and change that text.

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: NullReferenceException when using tk2dTextMesh
« Reply #3 on: September 13, 2013, 10:08:05 am »
And if you comment out all of the code in the PlayerObject Update function (everything referring to the text mesh) do you get the same error?  Usually the 'Object reference not set to an instance of an object' error is when you're trying to use a variable that has not been set.  I set up a simple test case using your PlayerObject code and the 'textMesh != null' line stops the rest of the code executing if no textMesh is attached to the game object (i.e. it worked correctly for me).

Probably the best thing for me to troubleshoot is if you create a scene with the bare minimum objects and code necessary to replicate the problem, export it and send it to support at unikronsoftware.com

thanks