How to pass the reference of a unityengine.object?

For any object derived from UnityEngine.Object, the keyword “this” returns null.
Don’t believe me?
Try this:
using UnityEngine;
using System.Collections;

public class TestClass : UnityEngine.Object{
	
	//class constructor
	public TestClass()
	{
		Debug.Log(this);	
	}
}

And then somewhere else, run this code:

TestClass testObject = new TestClass();
testObject.TestFunction;
//log will say "null"

I want my custom classes to inherit from UnityEngine.Object because I want them to be able to use the instantiate method to instantiate prefabs.
But I also want them to be able to pass themselves when they create custom objects, so that the objects that they create are able to reference the object that created them:

Class2 newObject = new Class2(this);

and then:

//constructor for class2
public Class2(Class1 dad)
{
owner = dad;
}

that way, Class2 can reference any method or property of Class1 like this:

owner.someVariable

Or at least it would be able to is “this” wasn’t returning null…

Is there a work around for this???

Your class name is Machinolation, so “public TestClass()” is not a constructor, you write “Public GameObject owner;” with a capital P, you try to call a TestFunction you didn’t even define.

Your code shouldn’t even compile, let alone run. My guess is you didn’t even try to run that code…

Also, if you really try to Log a custom object, you should override ToString… or you’ll never know if your parent class tries to troll you by returning the string “null” in ToString :smiley:

(For Serialization you should probably also implement GetHashCode, which I’m not doing here)

Anyway…

public class Machinolation : UnityEngine.Object {
	
	public GameObject owner;
	
	//class constructor
	public Machinolation()
	{
		Debug.Log(this);  
	}

	public override string ToString ()
	{
		return "BAM, it's [Machinolation]";
	}
}

then later:

Machinolation m = new Machinolation();

And you get the Log you expect.