About calling Resources assets

Hi, now in Fighter we don’t check if defaultWeapon is null,
so if we forget to reference it in the inspector we, of course, have this “null reference” we all hate (I think).

I tried to had this simple “if statement” in Awake():

        private void Awake()
        {
            if (defaultWeapon == null)
            {
                defaultWeapon = UnityEngine.Resources.Load<Weapon>("Unarmed");
            }

            currentWeapon = new LazyValue<Weapon>(SetupDefaultWeapon);
        }

It works fine, I just wonder if it isn’t a bad thing to call Resources in Awake()?

M. D.

No, there shouldn’t be any problem with calling this in Awake(). I would also include a Debug.Log to alert you that a character is missing it’s default weapon, though.

Debug.Log($"{name} does not have a default weapon set up in Fighter");
2 Likes

Thank you much for your response.

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

Privacy & Terms