Custom type Objects without hte " = new "

Hello guys, 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;

First of all RaycastHit and Color aren’t classes. They are structs and therefore valuetypes. Like LazyElephant already mentioned your own classes are not really different from built in classes.

Any kind of variable need to be initialized in some way before you can use it’s content. Class-variables or more general reference type variables just hold a reference to an instance of it’s type. So in your case the variable Bob is of type “Person”. If Person is a class then the variable Bob can hold a reference to an instance of your class Person. The default value for reference types is “null”, so it doesn’t reference any object. In your example you create a new instance of your Person class with “new” and store the reference to that instance in your variable Bob. So when using the variable Bob you can access that instance. That’s how classes work in general.

However the Unity editor has a feature for some supported classes, in general classes which can be serialized by Unity, where you don’t have to create a class manually but the editor might either create an instance for you or allows you to drag and drop an existing instance onto the variable in the inspector. In your example the Rigidbody type is a built-in component class. The variable, if nothing is assigned will also have the default value of “null” and using it will cause an exception since it doesn’t reference any object which could be accessed. Such a variable can be assigned in the inspector by dragging an existing component onto the variable in the inspector. You effectively store the reference to that component instance in the variable.

The case where the editor creates an instance for you only applies to custom classes which have the System.Serializable attribute. For example if you declare a type like this:

[System.Serializable]
public class Person
{
    public string name;
    public float age;
}

Whenever you have a variable of type Person in a MonoBehaviour, the Unity editor will automatically create an instance for you. So here you don’t need to create an instance manually with “new”. In general the Inspector can only view / edit things which are serializable by Unity. So the Person instance is actually serialized within the MonoBehaviour instance.

Finally value types like Vector3, RaycastHit, Color, Rect, ... can’t have “no value” like reference types since the variable directly hold the data. If you declare a Vector3 variable in a class, its members are automatically initialized with the default value. A Vector3 is just a struct with 3 float variables. The default value for a float is 0f. The Color struct is actually almost the same as a Vector4 but has different names for its variables (r, g, b, a instead of x, y, z, w). So the default value is (0f, 0f, 0f, 0f).

Since all those built-in Unity types are serializable, the inspector will show a color field and allows you to change the color in the inspector.

Note: This all only applies to member variables of a class. If you have local variable inside a method you actually have to assign a value before you can use the variable. Local variables aren’t initialized with the default value automatically.

void SomeMethod()
{
    Color col;
    Debug.Log(col); // error since "col" hasn't been initialized
}

Finally a short hint about MonoBehaviour derived classes. First of all a class is always a class and it always works the same way. The MonoBehavíour base class however has a special meaning to Unity. A MonoBehaviour is a component. Components are special classes which need to be attached to a GameObject instance. They can’t “live” on their own. That’s why you have to use the AddComponent method of the GameObject class to manually create an instance of a MonoBehaviour / Component class. You still can create an instance of a MonoBehavíour class with “new”, but you can’t do anything useful with the instance. Unity will also throw some warnings at you that you shouldn’t create Components with “new”.

When you drag a MonoBehaviour script file onto a gameobject in the editor, the editor actually calls AddComponent with the class that is defined in that file. The AddComponent method will create an instance of your class internally and attach it to the GameObject.

Unity classes behave in the same way as your custom class. In your example, you’re both declaring your person variable and assigning a reference to a new person on the same line. With the Unity classes in your example, you’re only creating the variables without assigning them to anything.

Before you could use the rigidbody, for example, you would have to assign a rigidbody object to it like
rb = new Rigidbody ();
Or by assigning an already existing object.

Edit: I was unaware that Color is actually a struct, so I changed the example to use the rigidbody.