I’m probably jumping the gun here, and this question will be answered very soon. I thought I’d do this as a simple self-challenge.
At the moment if we press the 4 or 5 key we get a Null Reference error.
Is this a reasonable way to do the null reference check, and should be done here in the ActiveInventory.cs script? Thoughts?
private void ChangeActiveWeapon()
{
if(ActiveWeapon.Instance.CurrentActiveWeapon != null)
{
Destroy(ActiveWeapon.Instance.CurrentActiveWeapon.gameObject);
}
if (!transform.GetChild(_activeSlotIndexNum).GetComponentInChildren<InventorySlot>())
{
ActiveWeapon.Instance.WeaponNull();
return;
}
//Null question
GameObject weaponToSpawn = transform?.GetChild(_activeSlotIndexNum)?.GetComponentInChildren<InventorySlot>()?.GetWeaponInfo()?.weaponPrefab;
//Exit Change Active Weapon if no prefab was found. Stays on the previous Weapon
if(weaponToSpawn == null) { return; }
GameObject newWeapon = Instantiate(weaponToSpawn, ActiveWeapon.Instance.transform.position, Quaternion.identity);
ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
newWeapon.transform.parent = ActiveWeapon.Instance.transform;
ActiveWeapon.Instance.NewWeapon(newWeapon.GetComponent<MonoBehaviour>());
}