- Home /
Instantiating a class
So in my code I try to create a new instance of a class I wrote, but it doesn't seem to work. The code is a bit like this:
class NewClass
{
function NewClass()
{
print("class created");
}
}
Obviously this is a simplified version of what I am trying to do, but I did write this example and it also did not work. The part that isn't working is that when I call the constructor, the code does not get called (in this example it will not print out). The code where my constructor is called looks like this:
//in some script
function OnTriggerEnter(other:Collider)
{
//some other code
new NewClass();
//more code
}
All of the code in the OnTriggerEnter method works and gets executed, but "class created" never gets printed out. I'm pretty new to programming so this might just be a stupid mistake but I've read over my notes and some stuff online and I'm sure this should work.
Thanks, Debug.Log let me see that my problem was elsewhere!
Answer by Eric5h5 · Dec 08, 2011 at 01:05 PM
Use Debug.Log rather than print, which only works in MonoBehaviours.