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

Unity中放置在Plate Kitchen Object上的GameObject被删除原因排查

问题定位与修复方案

核心问题原因

问题出在ClearCounter的Interact方法中,当玩家携带普通厨房物品、且当前Counter上是PlateKitchenObject时,代码逻辑会将玩家手中的物品作为配料添加到盘子中,随后调用DestroySelf()销毁该物品,这就是放到Plate上会被删除的直接原因:

if (GetKitchenObject().TryGetPlate(out plateKitchenObject))
{
   if (plateKitchenObject.TryAddIngredient(player.GetKitchenObject().GetKitchenObjectSO()))
   {
        player.GetKitchenObject().DestroySelf();
   }
}

额外代码混乱问题

你的代码存在结构冗余问题:KitchenObject类中重复定义了PlateCompleteVisual专属的KitchenObjectSO_GameObject结构体、plateKitchenObject字段以及OnIngredientAdded相关逻辑,这些内容本该属于PlateCompleteVisual类,会导致逻辑冲突和维护困难,必须先整理。

修复步骤

1. 清理KitchenObject类的冗余代码

删除KitchenObject类中以下冗余内容,这些逻辑应归属于PlateCompleteVisual:

[Serializable]
public struct KitchenObjectSO_GameObject
{
    public KitchenObjectSO kitchenObjectSO;
    public GameObject gameObject;
}

[SerializeField] private PlateKitchenObject plateKitchenObject;
[SerializeField] private List<KitchenObjectSO_GameObject> kitchenObjectSOGameObjectList;

private void Start()
{
    plateKitchenObject.OnIngredientAdded += PlateKitchenObject_OnIngredientAdded;

}

private void PlateKitchenObject_OnIngredientAdded(object sender, PlateKitchenObject.OnIngredientAddedEventArgs e)
{
    
    foreach (KitchenObjectSO_GameObject kitchenObjectSOGameObject in kitchenObjectSOGameObjectList)
    {
        if (kitchenObjectSOGameObject.kitchenObjectSO == e.kitchenObjectSO)
        {
            kitchenObjectSOGameObject.gameObject.SetActive(true);

            KitchenObjectType.IdentifyKitchenObject((KitchenObjectSO.ObjectType)(ObjectType)kitchenObjectSOGameObject.kitchenObjectSO.objectType);

        }
    }

}

2. 修正PlateCompleteVisual的逻辑

在PlateCompleteVisual类中补全事件绑定与处理逻辑:

private void Start()
{
    plateKitchenObject.OnIngredientAdded += PlateKitchenObject_OnIngredientAdded;
}

private void PlateKitchenObject_OnIngredientAdded(object sender, PlateKitchenObject.OnIngredientAddedEventArgs e)
{
    foreach (KitchenObjectSO_GameObject kitchenObjectSOGameObject in kitchenObjectSOGameObjectList)
    {
        if (kitchenObjectSOGameObject.kitchenObjectSO == e.kitchenObjectSO)
        {
            kitchenObjectSOGameObject.gameObject.SetActive(true);
            // 修正类型转换逻辑,避免强制转换错误
            KitchenObjectType.IdentifyKitchenObject((KitchenObjectType.ObjectType)e.kitchenObjectSO.objectType);
        }
    }
}

3. 调整ClearCounter的Interact逻辑(按需选择)

选项A:实现直接放置物品到盘子上

如果你希望物品直接挂载到Plate上而非作为配料合并,修改对应的分支逻辑:

else 
{
    // 替换原添加配料并销毁的逻辑
    player.GetKitchenObject().SetKitchenObjectParent(GetKitchenObject() as IKitchenObjectParent);
}

选项B:保留配料添加逻辑但增加区分条件

如果需要保留配料添加功能,可通过按键区分交互(比如按住Shift才添加配料),或增加判断条件避免误触发:

else 
{
    // 示例:按住Shift时才添加配料,否则直接放置
    if (Input.GetKey(KeyCode.LeftShift))
    {
        if (GetKitchenObject().TryGetPlate(out plateKitchenObject))
        {
            if (plateKitchenObject.TryAddIngredient(player.GetKitchenObject().GetKitchenObjectSO()))
            {
                player.GetKitchenObject().DestroySelf();
            }
        }
    }
    else
    {
        player.GetKitchenObject().SetKitchenObjectParent(GetKitchenObject() as IKitchenObjectParent);
    }
}

4. 修复KitchenObjectType的无效方法

删除KitchenObjectType中未实现的方法,避免运行时抛出异常:

// 删除这两个未实现的方法
internal static void IdentifyKitchenObject(KitchenObjectSO.ObjectType objectType)
{
    throw new NotImplementedException();
}

public static explicit operator KitchenObject.ObjectType(KitchenObjectType v)
{
    throw new NotImplementedException();
}

验证修复

完成修改后测试:

  • 选择直接放置逻辑:物品会正常挂载到Plate的跟随Transform下,不会被销毁
  • 选择带区分条件的配料逻辑:只有满足触发条件时,有效配料才会被合并销毁,其他物品正常放置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 07:27:49