2016-04-13 65 views
1

我将所有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

通常你不应该在变量中使用下划线。所以,它应该是userApple,userLemon等等。你需要仔细和正确地格式化你的代码。除非你对格式化绝对狂热,否则你可以永远不做***软件。 – Fattie

+0

顺便说一句,注意,要将文本解析为整数或浮点数并不那么容易。本质上,你不能使用“解析”。你必须使用TryParse。它需要相当多的工作,你需要一个扩展。 – Fattie

回答

1

这可能不是正确的解决方案尝试调用一个共同的错误来自Process Function的例程并产生循环,以便它运行每一帧。

+0

所以我必须将这个函数调用到另一个类的另一个函数中才能使它工作?有没有一个如何做到这一点的例子? – whoami

+0

不!现在使用像这样 void Process(){startCourotiune(CProcess); } ienumerator CProcess(){循环与yeild statemnet} 介意语法和了解协程以及它们如何帮助逐帧运行代码帧 – LumbusterTick

+0

笏这些是什么意思? 'UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)'的最佳重载方法匹配有一些无效参数 - 错误CS1503:参数'#1'无法将'方法组'表达式转换为类型'System.Collections.IEnumerator '--error CS0161:'Basket.CProcess()':并非所有的代码路径都返回一个值' – whoami

1

为了回顾Joe Blow所说的话:没有理由说这个for循环不应该工作,因为它完全独立于帧/时间。

这个小例子工作完全正常:
脚本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); 
      } 
     } 
    } 
} 

所有gameobjects被拖进了公示名单。

enter image description here

为gameobjects脚本是这样的:

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.

该按钮从上面调用的函数值为15enter image description here

当我点击按钮时,游戏物体的左边和中间列从场景和列表中删除 - 没有任何问题。

+0

雅。我使用了协程,它工作得很好。谢谢 – whoami