class constructor

i want to output what is inside constructor
that is:
IllithidIllithid

and the script returns me just:
Illithid

there is no point using constructor if i don’t have output
i can use class for that…

thanks for your help and time


function Update () {

var drazzen = Monster("Illithid");
print (drazzen.name);

}

class Monster 
	{
	var name;
	function Monster (n:String)
		{
		name = n;
		n = (name + n);
		print (n);
		
		}
	
	}

Have you tried using Debug.Log(n) rather than print? Seems to do what you want.

print is a static method of class Monobehaviour. Since all Unityscripts extend to Monobehaviour by default, you can call print without referencing its base class.

When you create your own classes however, they do not extend to Monobehaviour by default, therefore you need to reference the class of any static Monobehaviour function or variable you’re trying to access.

In your case, instead of

print(n);

you should type

MonoBehaviour.print(n);

Alternatively, you can do as the poster above me advised and call Debug.Log. Print(), when seen through reflection, is revealed to be a wrapper of Debug.Log(). Calling either of these will output the same result.

something is not working properly

i tried this way:


function Update () {

var drazzen = Monster("illithid");
//Debug.Log (drazzen.name);

}

class Monster 
	{
	var name;
	function Monster (n:String)
		{
		name = n;
		n = (name + n);
		Debug.Log (n);
		
		}
	
	}

OUTPUT:
illithidillithid

PROBLEM:
as you can see i got
//Debug.Log (drazzen.name);
but i have an output even if i don’t asak for it.


so i tried like this:

function Update () {

var drazzen = Monster("illithid");
Debug.Log (drazzen.name);

}

class Monster 
	{
	var name;
	function Monster (n:String)
		{
		name = n;
		n = (name + n);
		print(n);
		
		}
	
	}

OUTPUT:
illithid
PROBLEM:
i’m asking for illithidillithid

i tried with MonoBehaviour.print(n);
but same problem.

name = n + n…

although the motivation for doing what your trying to do is a mystery to me

i tried your way…
it’s not working.

i’m learning unity script and i’m trying to do easy stuff.
but if i’m not able to do easy things how can i go for tough ones

Define the type.

–Eric

YEAH!
thanks Eric5h5.

i tried something like 20 combinations and at last this is the only one working.

function Start () {

}

function Update () {

var drazzen = Monster("illithid");
print (drazzen.name);

}

class Monster 
	{
	var name : String;
	function Monster (n:String)
		{
		name = n + n;
		print(name);
		
		}
	
	}

print works like that from within class Monster? It shouldn’t…

class monster is siting inside a file that has a monobehaviour class

It doesn’t matter. Unless you do “class Monster extends MonoBehaviour”, it doesn’t extend monobehaviour even if it is inside a monobehaviour script. Extension is explicit, never implicit.