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);
}
}
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.
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.
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);
}
}
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.