Nicer syntax for adding components if null

This commit is contained in:
Stedd 2024-10-05 15:34:39 +02:00
parent b9d9e52c49
commit 53560fcc6b
2 changed files with 5 additions and 8 deletions

View File

@ -15,7 +15,6 @@ namespace AsteroidGame.Entities
[Header("UI")] [Header("UI")]
[SerializeField] protected string _uiFriendlyName; [SerializeField] protected string _uiFriendlyName;
#region Props #region Props
public string UiFriendlyName => _uiFriendlyName; public string UiFriendlyName => _uiFriendlyName;
@ -33,14 +32,12 @@ namespace AsteroidGame.Entities
private void InitializeDamageable() private void InitializeDamageable()
{ {
if (Damageable != null) return; Damageable ??= gameObject.AddComponent<Damageable>();
Damageable = gameObject.AddComponent<Damageable>();
} }
private void InitializeTargetable() private void InitializeTargetable()
{ {
if (Targetable != null) return; Targetable ??= gameObject.AddComponent<Targetable>();
Targetable = gameObject.AddComponent<Targetable>();
} }
private void AssignDamageable() private void AssignDamageable()

View File

@ -32,8 +32,8 @@ namespace AsteroidGame.Entities
private void InitializePower() private void InitializePower()
{ {
_power = gameObject.AddComponent<PowerBase>(); _power ??= gameObject.AddComponent<PowerBase>();
_power.SetConfig(_powerConfig); _power.Initialize(_powerConfig);
} }
protected void OnDisable() protected void OnDisable()