我将所有GameObjects放在一个列表下的场景中。以下是shelf.cs中用于删除游戏对象的函数。我将该函数分配给一个按钮,以便它获取输入值并找到哪个Gameobject的值小于输入值。然后删除。问题是每当我点击游戏预览中的按钮时,它都不会删除游戏对象。没有警告。我调试它是否收到所有输入,它确实。只是不删除GameObjects。
为什么?
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()
答案 0 :(得分:1)
这可能不是正确的解决方案尝试从Process Function调用一个协同例程并产生循环,以便它运行每一帧。