Initializing Serialized items as null means you don't have to ignore warnings

I am a total noob so perhaps someone can let me know if there is a major downside to this.

I don’t like the idea of having to ignore warnings if there is an easy way to get rid of them. The warnings that Rick ignores in this video are warnings that visual studio sends because there are items in the code that don’t have a value in the code before it runs, but we assign through the inspector.

To fix this I just initialize serialized items as null and then I no longer get these errors and as soon as you run the game the values are updated by the inspector as usual so I think this is a clean solution.

For example in the current Weapon script at the top we have:

    [SerializeField] ParticleSystem muzzleFlash;
    [SerializeField] GameObject hitExplosion;
    [SerializeField] AudioClip fireSound;
    [SerializeField] Camera fPCamera;

instead I list them like this:

    [SerializeField] ParticleSystem muzzleFlash = null;
    [SerializeField] GameObject hitExplosion = null;
    [SerializeField] AudioClip fireSound = null;
    [SerializeField] Camera fPCamera = null;

This way they are initialized and will no longer give warnings and as far as I can tell there are no problems so far since everything runs great.

3 Likes

That’s right! Nice finding.

It indeed stops the warnings, just remember that you can’t set all the variables types to null, if you have floats, for instance, you must set them to 0, same with bool, you have to set them to false or true, there are other variable types that won’t accept null, just keep that in mind.

It’s also nice to have some understanding of what is going in the background to know how to set the variables correctly since there are some that might be confusing for beginners, one example is LayerMask, this won’t accept null, instead you have to set it to 0, Why? I let you discover that by yourself, is actually quite interesting.

1 Like

Oh cool, setting a Layermask to 0 sets it to default. I didn’t know that, thanks for pointing me to look more into layermasks!

Hearing Rick suggest disabling warnings irked me so I had to say something about it, but I’ll refrain to open a new topic just to bemoan about it :stuck_out_tongue:

BTW if Ben is anything like me he probably had a twitchy eye for a few minutes after hearing that suggestion…

Anyway, you may be a noob but you were on point. The idea of muting warnings seems terrible to me. They are annoying sometimes, and this particular case is a good example of probably-excessively-prudent warnings, but as you found out, you can get rid of them by actually fixing what they complain about (if I remember correctly there’re also ways to disable specific warnings, BTW).

You also wonder if there’s any downside to this, and no, there’s not because initialising these variables to null is exactly what the compiler will do for us otherwise. That’s their default values. As @Yee points out, that can’t be done for every type (only for references), but every type does have a default value usually in the ballpark of zero (e.g. Vector3's default to Vector3.zero, enums default to the first value, and so on).

Ever since that warning poped up for me I started explicitly initialising every single variable, which I think is not a bad habit anyway. It’s redundant but it has no drawbacks and I always favour explicit code anyway, and that has been of use to me in the past.

Privacy & Terms