Need help solving the issue with my level system, the code consists of making the level 1 prefab gameobject be destroyed when leveling up to level 2 to avoid overlapping, but despite my efforts It still keeps getting overlapped.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public List<WeaponController> weaponSlots = new List<WeaponController>(6);
public int[] weaponLevels = new int[6];
public List<PassiveItem> passiveItemSlots = new List<PassiveItem>(6);
public int[] passiveItemLevels = new int[6];
public void AddWeapon(int slotIndex, WeaponController weapon) //Add a weapon to a specific slot
{
weaponSlots[slotIndex] = weapon;
weaponLevels[slotIndex] = weapon.weaponData.Level;
}
public void AddPassiveItem(int slotIndex, PassiveItem passiveItem) //Add a passive item to a specific slot
{
passiveItemSlots[slotIndex] = passiveItem;
passiveItemLevels[slotIndex] = passiveItem.passiveItemData.Level;
}
public void LevelUpWeapon(int slotIndex)
{
if(weaponSlots.Count > slotIndex)
{
WeaponController weapon = weaponSlots[slotIndex];
if (!weapon.weaponData.NextLevelPrefab)
{
Debug.LogError("NO NEXT LEVEL FOR " + weapon.name); //Checks if there is a next level for the weapon
return;
}
GameObject upgradedWeapon = Instantiate(weapon.weaponData.NextLevelPrefab, transform.position, Quaternion.identity);
upgradedWeapon.transform.SetParent(transform); //Sets the weapon to be a child of the player
AddWeapon(slotIndex, upgradedWeapon.GetComponent<WeaponController>());
Destroy(weapon.gameObject); //Destroys previous weapon from the scene to prevent overlapping
weaponLevels[slotIndex] = upgradedWeapon.GetComponent<WeaponController>().weaponData.Level; //Makes sure we have the correct weapon level
}
}
public void LevelUpPassiveItem(int slotIndex)
{
if(passiveItemSlots.Count > slotIndex)
{
PassiveItem passiveItem = passiveItemSlots[slotIndex];
if (!passiveItem.passiveItemData.NextLevelPrefab)
{
Debug.LogError("NO NEXT LEVEL FOR " + passiveItem.name); //Checks if there is a next level for the passive item
return;
}
GameObject upgradedPassiveItem = Instantiate(passiveItem.passiveItemData.NextLevelPrefab, transform.position, Quaternion.identity);
upgradedPassiveItem.transform.SetParent(transform); //Sets the passive item to be a child of the player
AddPassiveItem(slotIndex, upgradedPassiveItem.GetComponent<PassiveItem>());
Destroy(passiveItem.gameObject); //Destroys previous passive item from the scene to prevent overlapping
passiveItemLevels[slotIndex] = upgradedPassiveItem.GetComponent<PassiveItem>().passiveItemData.Level; //Makes sure we have the correct passive item level
}
}
}