using UnityEngine; using System.Collections.Generic; public class WeaponManager : MonoBehaviour { public Transform weaponHolder; // Parent object holding all weapons private List<GameObject> weapons = new List<GameObject>(); private int currentWeaponIndex = 0; private HashSet<string> unlockedWeapons = new HashSet<string>(); // Stores unlocked weapon names void Start() { if (weaponHolder == null) { Debug.LogError("❌ WeaponHolder is not assigned in WeaponManager! Assign it in the Inspector."); return; } foreach (Transform weapon in weaponHolder) { weapons.Add(weapon.gameObject); weapon.gameObject.SetActive(false); // 🔥 Hide weapons at start } if (weapons.Count > 0) { UnlockWeapon(weapons[0].name, weapons[0]); // Unlock the first weapon (Pistol) } } public void UnlockWeapon(string weaponName, GameObject weaponPrefab) { Debug.Log($"🔫 Trying to unlock {weaponName}..."); // Check if the weapon is already in the inventory foreach (GameObject weapon in weapons) { if (weapon.name == weaponName) { if (!unlockedWeapons.Contains(weaponName)) { unlockedWeapons.Add(weaponName); weapon.SetActive(true); Debug.Log($"✅ {weaponName} is now unlocked and usable!"); } else { Debug.Log($"⚠️ {weaponName} is already unlocked!"); } return; } } // 🔥 If the weapon isn't in the list, instantiate it in weaponHolder if (weaponPrefab != null) { GameObject newWeapon = Instantiate(weaponPrefab, weaponHolder); newWeapon.name = weaponName; newWeapon.transform.SetParent(weaponHolder); newWeapon.SetActive(false); weapons.Add(newWeapon); unlockedWeapons.Add(weaponName); // 🔥 Fix: Ensure the weapon is marked as unlocked Debug.Log($"🆕 {weaponName} has been added to the inventory!"); // 🔥 Log the inventory contents Debug.Log("📜 Current Inventory:"); foreach (GameObject w in weapons) { Debug.Log($"- {w.name}"); } } else { Debug.LogError($"❌ weaponPrefab is NULL for {weaponName}! Assign it in WeaponPickup."); } } void Update() { HandleWeaponSwitching(); } void HandleWeaponSwitching() { if (Input.GetKeyDown(KeyCode.Alpha1)) SwitchToWeapon(0); // Pistol if (Input.GetKeyDown(KeyCode.Alpha2)) SwitchToWeapon(1); // Shotgun if (Input.GetKeyDown(KeyCode.Alpha3)) SwitchToWeapon(2); // Assault Rifle if (Input.GetKeyDown(KeyCode.Alpha4)) SwitchToWeapon(3); // Energy Gun if (Input.GetKeyDown(KeyCode.Alpha5)) SwitchToWeapon(4); // Rocket Launcher if (Input.GetKeyDown(KeyCode.Alpha6)) SwitchToWeapon(5); // Grenade Launcher if (Input.GetKeyDown(KeyCode.Alpha7)) SwitchToWeapon(6); // Railgun } void SwitchToWeapon(int index) { if (index >= 0 && index < weapons.Count) { Debug.Log($"🔄 Trying to switch to weapon at index {index}..."); // 🔥 Fix: Check if the weapon is unlocked if (unlockedWeapons.Contains(weapons[index].name)) { foreach (GameObject weapon in weapons) { weapon.SetActive(false); } weapons[index].SetActive(true); currentWeaponIndex = index; Debug.Log($"✅ Switched to {weapons[index].name}!"); } else { Debug.Log($"❌ Weapon {weapons[index].name} is not unlocked!"); } } else { Debug.LogError($"❌ Invalid weapon index: {index}. Total weapons: {weapons.Count}"); } } }