- Home /
How can I use "print" from within a custom class?
I have created a custom class, but the print function doesn't seem to work from within the function.
I have main script that looks like this:
function Start (){ tc = new TestClass(); print(tc.getString());
}
And my class looks like this:
class TestClass {
function TestClass ()
{
}
function getString(){
print("getString");
return "string";
}
}
I see "string" in the console, but not "getString". Is there something I need to do to get
print("getString");
to display in the console?
Answer by duck · May 15, 2010 at 09:26 PM
'print' is actually a function on the MonoBehaviour class - which is why you can only use it in scripts which inherit from monobehaviours (if you don't explicitly declare your class body in javascript, unity automatically makes your script inherit from MonoBehaviour).
However, 'print' is simply a wrapper for Debug.Log which you can use from anywhere, so use that instead!
Answer by VR Man · May 17, 2010 at 01:43 PM
You should be able to use the Debug.Log("Hey just like print()!"); type command, and it has a lot of nice options for putting stuff to the console. Be well, -=VRMan3d.com=-
Your answer
![](https://koobas.hobune.stream/wayback/20220613000315im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do you get console messages in order? 0 Answers
Health value isn't subtracting correctly 1 Answer
Why print() only print the same string once? 1 Answer
How to print Byte 1101010 version of a float? 1 Answer
Debug.Print not working 3 Answers