Classes questions

Hi, I’m trying to create a debug class that when instanced, will create an object with a textmesh so I can display debug text where ever the object is. This is where I am up to so far (just creating the object and passing the text) and I’m putting the call in the update function jsut for testing.

Here is the code:

private var numberofObjects: int =0;

class DebugText
{
	var passedtext;
	
	//This is the constructor
	function DebugText(textToDisplay: String)
	{
		//Incrememnt number of objects being created so we can name them individually
		numberofObjects++; 
		
		//Generate name
		var objectName : String = "DebugText"+numberofObjects;
		
		//Need to add 3D Text gameobject 
		DebugTextObject = new GameObject (objectName);
		DebugTextObject.AddComponent ("TextMesh");
		currentMesh= DebugTextObject.GetComponent(TextMesh);
		currentMesh.text=textToDisplay;

	}
}

function Update()
{
	ant = DebugText("anT");

}

A couple of questions:

  1. Unity complains that numberofObjects is an unknown identifier - I’ve no idea why.
  2. I’m concerned that after I create the object and add the component textmesh, that I’m accessing it incorectly - I previously tried DebugTextObject.TextMesh.text but it said textmesh wasnt a member of gameobject…

I feel I’m tieing myself in knots over this one. As I side note, I’m also noticing that Unity isn’t consistently reporting the same error. ie. I can run this code again and it wouldnt necessarily report that numberofObjects is an unknown identifier…odd.

Any help would be greatly appreciated.

I have never used .js but maybe your variable must be declared as public or protected?

It looks like there may be more than one error there. Its late so I’ll just find one for you :slight_smile: I would have to do too much poking around to solve the rest, I dont do JS.

This is the easy one:

class DebugText
{
var passedtext;
private var numberofObjects: int =0; // this should be in the class, not outside