I am trying to create a game by creating objects of custom classes that then instantiate and control prefabs.
(I suppose the intended way to use unity is to instantiate prefabs that have controlling scripts attached as components.)
I noticed that the keyword “this” returns the GameObject that the class script is a component of.
This means that when I create an object using:
“Customer object1 = new Customer();”
the “this” keyword returns “null” when it is used inside the “Customer” class definition.
Under normal circumstances, wouldn’t the C# “this” method return the instance ID of “object1”???
Is unity changing the definition of “this” to refer to the containing GameObject?
I am trying to pass the instance of the object so that other objects can directly refer to it and its properties and methods. Is there some other way to do it?
I tried researching this myself and I found similar posts, but all the answer were along the lines of :
“No. You are wrong. ‘this’ by definition can never return ‘null’. You made a mistake in your code when logged the return of ‘this’.”
However, this explanation fails to account for my many null reference exceptions…