Class instances

Hi,

Actually, we do not need to create an instance. We need a reference (“link”) to an existing object/instance of Class A. ClassName objectName = new ClassName(); might create an object of ClassName but that’s not the reason why you are able to access the object of type ClassName. The reason is that an object reference got assigned to objectName. As long as the variable references an object, you can access the object via the variable. “C#” does not care where the reference comes from.

As aforementioned, the crucial part is the reference. GetComponent<Rigidbody>() returns a reference (or null). If you assign the returned reference to rb, you will be able to access the assigned Rigidbody object via rb.

Is this what you wanted to know?


See also: