2016-11-18 55 views
0

這可能看起來很幼稚,但我只是沒有得到它。 這個程序有不同的按鈕,顯示或隱藏畫布和相關信息,在這種情況下,換油。Unity3d摧毀任何存在之前存在的對象

當按下按鈕時,它將打開換油畫布,從數據庫獲取信息並顯示來自實例化預製件的信息。這工作正常。當您單擊相同的按鈕或另一個按鈕並返回到同一畫布時,問題就出現了。信息是雙倍或三倍或四倍,你明白了。每當你回到它時,它就會倍增。

顯然,遊戲對象必須實例化之前被銷燬,而是:

  1. 我不知道該怎麼檢查;它是用於實例化還是預製的臨時變量?我已經嘗試在腳本的頂部定義它,在任何其他時間,我都會遇到未分配使用的錯誤。

  2. 我不確定調用函數的最佳時間。一旦你按下按鈕,我就會猜測。

    public void ShowAllOilChanges() 
    { 
        for(int i = 0; i< OilChangeList.Count; i++) 
         { 
          ocObj = Instantiate(OilChangePrefab); 
          OilChange tmpOilChange = OilChangeList[i]; 
          ocObj.GetComponent<OilChangeScript>().DisplayOilChanges("Date of Service: " +tmpOilChange.ServiceDate, "Place of Service: " +tmpOilChange.Location, "Mileage: " +tmpOilChange.Mileage, "Labor: " +tmpOilChange.Labor, "Oil Brand: " +tmpOilChange.OilBrand, "Oil Price: " +tmpOilChange.OilPrice, "Filter Brand: " +tmpOilChange.FilterBrand, "Filter Price: " +tmpOilChange.FilterPrice, "Purchase Place: " +tmpOilChange.OilFilterPurchaseLocation); 
          ocObj.transform.SetParent(OilDisplayparent); 
    
         } 
    } 
    
    public void DestroyOilChange() 
    { 
        Debug.Log("Before" +ocObj); 
    
        Destroy(ocObj); 
        Debug.Log("After" +ocObj); 
    } 
    
    public void OpenOilWindow() 
    { 
    
         if(windowOpen==false) 
         { 
          windowOpen=true; 
          OilCanvas.enabled=true; 
          BatteryCanvas.enabled=false; 
          BrakeCanvas.enabled=false; 
          TireCanvas.enabled=false; 
          PlugCanvas.enabled=false; 
          MufflerCanvas.enabled=false; 
          CustomCanvas.enabled=false; 
          DestroyOilChange(); 
    
         } 
         else if(windowOpen==true) 
         { 
          windowOpen=false; 
          OilCanvas.enabled=false; 
    
         } 
    } 
    

如果有人能走路我通過,我將不勝感激。

+0

有太多的缺失信息來協助。我甚至不知道這些方法在哪些文件中,以及誰調用它們。必須展示更多完整的流程(IMO)供任何人協助。 – TDWebDev

回答

0

我正在看着這個倒退。我做的第一件事就是爲需要刪除的各種類型的對象創建標籤。然後我將它們添加到它們各自的預製件中。當您打開一個畫布(窗口)並關閉一個畫布時,會調用此函數。

public void DestroyObjects(string tag) 
{ 
    GameObject[] Obj = GameObject.FindGameObjectsWithTag(tag); 
    if(Obj==null) 
     return; 
    else 
    { 
     Debug.Log(Obj.Length); 
     for(int i=0; i< Obj.Length; i++) 
     { 
      Destroy(Obj[i]); 
     } 
     Debug.Log(tag + "Objects Destroyed!"); 
    } 

} 

希望這可以幫助像我這樣的人。

相關問題