- Home /
Why is print() only in MonoBehaviour?
Why is print only in MonoBehaviour (and not using UnityEngine(?
Specifically, if you don't inherit from MonoBehaviour but still have using UnityEngine
this statement won't work: print("hi");
and you have to use Debug.Log("hi");
Answer by Kiwasi · Oct 30, 2014 at 01:23 AM
Print is a wrapper method that calls Debug.Log. Its a static member of MonoBehaviour. So it can be called from within a MonoBehaviour. If you have a real problem with Debug.Log you can still use print like so:
MonoBehaviour.print("This lets me avoid typing Debug.Log");
print() saves typing... I ended up creating a new static class called p... p.print() for Debug.Log()
Or you can just use:
$$anonymous$$onoBehaviour.print();
in any non $$anonymous$$B class.
@fafase: Hmm, but that doesn't saves typing ;D Personally i always use Debug.Log. i was never a friend of "print" (maybe because it's too "C" like). Depending on the project i also use a custom logging system.
Thanks to intelliSense the "typing" is already reduced to a $$anonymous$$imum.
Also keep in $$anonymous$$d that "print" only allows printing a message while "Debug.Log" can take an optional context reference.
To finally answer the "why not using UnityEngine": A namespace can't contain methods, it has to be inside a class.