无法删除游戏对象

Cannot to delete the GameObject

我把场景中的所有GameObject都放在了一个列表下。以下是 shelf.cs 中用于删除游戏对象的函数。我将该功能分配给一个按钮,以便它接受输入值并找到哪个游戏对象的值小于输入值。然后删除。问题是每当我在游戏预览中单击按钮时,它都不会删除游戏对象。没有警告。我调试它是否收到所有输入,它收到了。只是不删除游戏对象。

为什么?

public void Process(){
    int user_apple,user_lemon,user_watermelon;

    user_apple = int.Parse (input_apple.text);
    user_lemon = int.Parse (input_lemon.text);
    user_watermelon = int.Parse (input_watermelon.text);

    Debug.Log (user_apple+" "+user_lemon+" "+user_watermelon);

    for(int i = players.Count - 1; i >= 0; i--)
    {   if(players[i].name == "Lemon")
        {
            if(players[i].GetComponent<Apple>().weight <= user_apple)
            {   Debug.Log ("wat u want");
                Destroy(players[i]);
            }players.RemoveAt(i);

        }
    }
}

如果我这样说的话,

public void Process(){
    int user_apple,user_lemon,user_watermelon;

    user_apple = int.Parse (input_apple.text);
    user_lemon = int.Parse (input_lemon.text);
    user_watermelon = int.Parse (input_watermelon.text);

    Debug.Log (user_apple+" "+user_lemon+" "+user_watermelon);
    if(players[2].GetComponent<Lemon>().weight <= user_apple)
    {   Destroy(players[2]);
        players.RemoveAt(2);
    }
}

会出现如下错误

FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629)
Basket.Process () (at Assets/scripts/Basket.cs:77)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:110)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:575)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:717)
UnityEngine.Events.UnityEvent.Invoke () (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

这可能不是正确的解决方案,请尝试从处理函数调用协程并产生循环,以便它在每一帧运行。

只是回顾一下 Joe Blow 所说的话:没有理由为什么这个 for 循环不工作,因为它完全独立于 frames/time。

这个小例子运行得非常好:
脚本 ScriptHolder 被分配给一个空的游戏对象(见下面的屏幕)并且看起来像这样:

using UnityEngine;
using System.Collections.Generic;

public class ScriptHolder: MonoBehaviour
{
    public List<GameObject> theGameObjects;

    public void TheButtonFunction(int weight)
    {
        for(int i = theGameObjects.Count - 1; i >= 0; i--)
        {
            if(theGameObjects[i].GetComponent<TheObject>().objectWeight <= weight)
            {
                Destroy(theGameObjects[i]);
                theGameObjects.RemoveAt(i);
            }
        }
    }
}

所有游戏对象都被拖入 public 列表。

游戏对象的脚本如下所示:

using UnityEngine;
using System.Collections;

public class TheObject : MonoBehaviour
{
    public string objectName = "";
    public int objectWeight = 0;
}

所有 GameObject_A 都命名为 A 并且权重为 10(在检查器中完成),GameObject_BB,权重为15GameObject_CC,权重为 20。列从左到右为 A 到 C。

按钮从上面调用函数,值为 15

当我单击该按钮时,游戏对象的左列和中间列将从场景和列表中删除 - 没问题。