Is there a reason that you are storing the Weapon Info in the prefab?
The Weapon Info ScriptableObject has the prefab to instantiate. Why not pass that to the Active Weapon New Weapon instead.
You can store the current active weapon as IWepaon, Instatiate from within the Active Weapon itself, and you only have to cast from MonoBeaviour to IWeapon once
public class ActiveWeapon : Singleton<ActiveWeapon>
{
private IWeapon _currentActiveWeapon;
private GameObject _currentActiveWeaponGameObject;
private bool _hasCurrentWeapon;
// Other Properties/Variables and Methods
private void DestroyCurrentWeapon()
{
_hasCurrentWeapon = false;
_currentActiveWeapon= null;
Destroy(_currentActiveWeaponGameObject);
_currentActiveWeaponGameObject = null;
}
public void NewWeapon(WeaponInfo weaponInfo)
{
if (_hasCurrentWeapon)
DestroyCurrentWeapon();
var weaponToSpawn = weaponInfo.weaponPrefab;
if (weaponToSpawn == null) return;
_currentActiveWeaponGameObject = Instantiate(weaponToSpawn, transform.position, Quaternion.identity);
transform.rotation = Quaternion.Euler(0, 0, 0);
// ReSharper disable once Unity.InefficientPropertyAccess
_currentActiveWeaponGameObject.transform.parent = transform;
_currentActiveWeapon = _currentActiveWeaponGameObject.GetComponent<MonoBehaviour>() as IWeapon;
_hasCurrentWeapon = _currentActiveWeaponInfo != null;
if (!_hasCurrentWeapon)
{
Destroy(_currentActiveWeaponGameObject);
_currentActiveWeaponGameObject = null;
}
AttackCooldown();
_timeBetweenAttacks = !_hasCurrentWeapon ? 0 : weaponInfo.weaponCooldown;
}
public void WeaponNull()
{
AttackCooldown();
if (_hasCurrentWeapon)
DestroyCurrentWeapon();
_hasCurrentWeapon = false;
_currentActiveWeapon = null;
_currentActiveWeaponGameObject = null;
_timeBetweenAttacks = 0f;
}
}
In ActiveInventory
private void ChangeActiveWeapon()
{
var activeWeapon = ActiveWeapon.Instance;
var inventorySlot = transform.transform.GetChild(_activeSlotIndexNum).GetComponent<InventorySlot>();
if (inventorySlot == null)
{
activeWeapon.WeaponNull();
return;
}
activeWeapon.NewWeapon(inventorySlot.WeaponInfo);
}