You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity商店系统开发问题:购买武器替换与武器切换脚本冲突

Solution for Unity Weapon Replacement + Switching System Issue

Hey there! Let's tackle this weapon replacement problem you're facing while working with your Unity shop system and existing weapon switching script. I've dealt with similar setups before, so here's a practical, step-by-step fix that’ll keep both systems working in sync:

1. First, Standardize Your Weapon Management Structure

Make sure your weapon switching script has clear, accessible references to track owned weapons and the currently active one. This is the foundation for making replacement work smoothly:

  • Add these variables to your WeaponSwitcher script:
    public List<GameObject> ownedWeapons; // Tracks all weapons the player has
    public int currentWeaponIndex; // Tracks which weapon is active (0 = 1 key, 1 = 2 key, etc.)
    public Transform weaponHolder; // Empty transform to parent all weapons to
    
  • Optional but helpful: Create a basic Weapon component to attach to every weapon prefab, with a field like public int keybindSlot; (1/2/3) to map weapons to specific keys.

2. Implement the Purchase Button Logic

In your shop's buy button click handler, add this logic to replace the current weapon while updating the switcher's state:

public void OnBuyNewWeapon(GameObject newWeaponPrefab)
{
    // 1. Get and destroy the currently active weapon
    GameObject currentWeapon = ownedWeapons[currentWeaponIndex];
    if (currentWeapon != null)
    {
        Destroy(currentWeapon);
    }

    // 2. Remove the old weapon from the switcher's list
    ownedWeapons.RemoveAt(currentWeaponIndex);

    // 3. Spawn the new weapon and parent it to the weapon holder
    GameObject newWeapon = Instantiate(newWeaponPrefab, weaponHolder.position, weaponHolder.rotation);
    newWeapon.transform.parent = weaponHolder;
    newWeapon.SetActive(false); // Start hidden until switched to

    // 4. Add the new weapon to the switcher's list (replace the same slot)
    ownedWeapons.Insert(currentWeaponIndex, newWeapon);

    // 5. Immediately switch to the new weapon
    GetComponent<WeaponSwitcher>().SwitchWeapon(currentWeaponIndex);
}

3. Update Your Weapon Switcher Script for Compatibility

Make sure your SwitchWeapon method handles empty slots and properly toggles weapon visibility:

public void SwitchWeapon(int index)
{
    // Hide all owned weapons first
    foreach (GameObject weapon in ownedWeapons)
    {
        if (weapon != null)
            weapon.SetActive(false);
    }

    // Activate the selected weapon (if it exists)
    if (index >= 0 && index < ownedWeapons.Count && ownedWeapons[index] != null)
    {
        ownedWeapons[index].SetActive(true);
        currentWeaponIndex = index;
    }
}

// Update method for key presses (add null checks!)
void Update()
{
    if (Input.GetKeyDown(KeyCode.Alpha1) && ownedWeapons.Count > 0 && ownedWeapons[0] != null)
    {
        SwitchWeapon(0);
    }
    if (Input.GetKeyDown(KeyCode.Alpha2) && ownedWeapons.Count > 1 && ownedWeapons[1] != null)
    {
        SwitchWeapon(1);
    }
    if (Input.GetKeyDown(KeyCode.Alpha3) && ownedWeapons.Count > 2 && ownedWeapons[2] != null)
    {
        SwitchWeapon(2);
    }
}

4. Critical Notes to Avoid Bugs

  • Parent All Weapons: Always spawn new weapons under the same weaponHolder transform to keep positions/rotations consistent.
  • Null Reference Checks: Every time you access ownedWeapons or a weapon object, add a null check to prevent runtime crashes.
  • Slot Consistency: If you want to replace a specific key's weapon (e.g., always replace the 1-key weapon), skip removing from the list and directly overwrite the corresponding index instead of inserting.

内容的提问来源于stack exchange,提问作者Mir Usmanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:53:56