2016-04-13 64 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