When do we need to use/not use New keyword when creating SerializeField's

On “How To Use GetComponent”, when we create the Color32 [SerializeField], why is it that this time we need to set it equal to “new Color32”? Say compared to when we created the [SerializeField] float delayBeforeDelete, in which case we could just = 1f, insead = new float(1f) (or something like that).



Do you know constructors? They are special methods that create objects for us. It requires the new keyword. If we wrote pure C# code, we would use new and constructors “everywhere” with one exception: For the primitive types in C#, you do not need any constructor. Float is a primitive type.

However, we do not write pure C# code. Instead, we use Unity classes. Unity wants to create its own objects. For this reason, you basically never see something like Player player = new Player(); in Unity code. The Player class, in this case, inherits from MonoBehaviour, which is a Unity class.

One more thing: Note that I explicitely wrote about Unity classes. Classes are so-called reference types because we don’t work with objects directly but reference them. In C#, there are also structs and enums, which are value types. Vector3 and Color32 are structs. We know that from the API (and from our script editor). We may call constructors to create a new struct object.

If you find this confusing, the best way to learn this is to simply memorise the C# value types. That list is short. Everything else is a reference type. And then you only have to know if the reference type is a Unity type or “another” type. Of course, you don’t have to know all Unity and C# classes by heart. There are several tens of thousands. Your script editor will tell you if something is a Unity class or not.

Did this clear it up for you?

See also:


Okay I think I mostly follow you.

I don’t really know constructors, but I’ve been reading up a little in the Microsoft documentation to get a small handle on the value type vs reference type, and it’s coming along.

Thanks for your explanation Nina.

Constructors are just methods that create objects. Player player;, for example, is an “empty” variable. Its value is null meaning “no object reference”.

To create a new Player object in pure C#, we would simply execute new Player();. The name of the constructor matches the object type.

Since your computer cannot remember anything and since you don’t want to waste your time looking for nameless data in memory, you usually assign an object to a variable:

Player player = new Player();
Player gary = new Player();
Player rick = new Player();

And that’s it.

For further information on this subject, you could take a look at this website. Read only the parts about the default, private and public constructors. The other parts are irrelevant because we don’t use these things in this course. You may read them anyway, though, if you want.


This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms