0

I was wondering why is it that when I create a custom class, I have to declare an object like this:

 Person Bob = new Person;

But with Unity's custom classes I don't; Example:

 puiblic RaycastHit hit;
 public Color myColor;
 public RigidBody rB;
NakkyGraphics
  • 193
  • 1
  • 2
  • 5
  • [Read This](http://stackoverflow.com/questions/2330767/what-is-the-difference-between-instantiated-and-initialized/11423017#11423017) – radarbob Dec 13 '15 at 01:16
  • `Person Bob = new Person` won't compile anyway. `RigidBody` **is** a class so you **will** have to call `new` so not sure where you got that idea from –  Dec 13 '15 at 01:36
  • I'm voting to close this question as off-topic because question is so simple it is unlikely to have any use for future readers –  Dec 13 '15 at 01:37

2 Answers2

0

The latter example is just creating a variable that can hold an instance of an object. It's not actually creating an instance of anything.

If those variables happen to be structs, they'll have a default value.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
0

RaycastHit and Color are not classes, they are structs. This means that they are declared just like an int would be, and they can be immediately put to use without the need for new.

RigidBody, on the other hand, is a class, so this declaration does not instantiate an object, it just declares a reference to an object, which will have to be instantiated or otherwise obtained further down.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142