2016-11-21 112 views
0

我在我的遊戲中實施了煙霧預製。這個想法是創建預製件的3個不同的實例(在不同的位置),然後根據切換按鈕銷燬它們。這個問題沒有被破壞,我不知道爲什麼。銷燬預製實例

這是我的腳本:

class Smoke1 : MonoBehaviour 
    { 
    public GameObject myPrefab; 
    public GameObject canvasObject; 

    public static GameObject newSmoke1; 
    public static GameObject newSmoke2; 
    public static GameObject newSmoke3; 

    public int toggle1; 
    public int toggle2; 
    public int toggle3; 

    public Vector3 smokeposition1 = new Vector3 (397, -394, 90); 
    public Vector3 smokeposition2 = new Vector3(414, -402, 90); 
    public Vector3 smokeposition3 = new Vector3(432, -410, 90); 

    string newSmoke; 


    void Start() 
     { 
     i = 1; 
     toggle1 = 2; 
     toggle2 = 2; 
     toggle3 = 2; 
     newinstance(smokeposition1, newSmoke1); 
     newinstance(smokeposition2, newSmoke2); 
     newinstance(smokeposition3, newSmoke3); 
     } 

    void Update() 
     { 

     togglecheck(ThermoElectric.t1Bool, toggle1, newSmoke1, smokeposition1); 
     togglecheck(ThermoElectric.t2Bool, toggle2, newSmoke2, smokeposition2); 
     togglecheck(ThermoElectric.t3Bool, toggle3, newSmoke3, smokeposition3); 
     } 

    void newinstance(Vector3 smokeposition, GameObject smokeinstance) 
     { 
     smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject; 
     smokeinstance.transform.SetParent(canvasObject.transform, false); 
     smokeinstance.transform.localScale = new Vector3(1, 1, 1); 

     smokeinstance.GetComponent<ParticleSystem>().startColor = new Color(0.1f, 0.1f, 0.1f, 1); 

     } 

    void togglecheck(bool turbine, int togglei, GameObject smokeinstancet, Vector3 smokeposition) 
     { 

     if (turbine == true && togglei == 1) 
      { 
      newinstance(smokeposition, smokeinstancet); 
      togglei = 2; 
      } 

     if (turbine == false && togglei == 2) 
      { 
      Destroy(smokeinstancet, 0.1f); 
      togglei = 1; 
      } 
     } 
} 

的布爾變量正在和如果條件得到滿足,但destroyed..Can你幫組合屋沒有得到?

+0

https://docs.unity3d.com/ScriptReference/Object.Destroy。html 你什麼時候檢查過你的元素是否已經被刪除? _「實際的物體破壞總是延遲到當前更新循環之後,但總是在渲染之前完成。」_ – TripleEEE

+0

當遊戲運行時,我看到當我更改切換時,預製件不會被破壞。 –

+0

我看不出延遲的原因,循環應該馬上完成。 –

回答

1

布爾正在工作,如果條件符合,但預製件 不會被破壞..你能幫忙嗎?

。你不應該破壞預製件。您應該只銷毀使用Instantiate創建的GameObject。

。從GameObject中刪除靜態圖。我相信你不需要那個。

如果你是100%肯定肯定的,如果條件得到滿足,則newinstance功能的問題。如果你不知道,把裏面Debug.Logif (turbine == false && togglei == 2)驗證此條件沒有因爲見面,Destroy(smokeinstancet, 0.1f);不會被稱爲。

問題與newinstance函數。

void newinstance(Vector3 smokeposition, GameObject smokeinstance) 
{ 
    smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject; 
    smokeinstance.transform.SetParent(canvasObject.transform, false); 
    smokeinstance.transform.localScale = new Vector3(1, 1, 1); 

    smokeinstance.GetComponent<ParticleSystem>().startColor = new Color(0.1f, 0.1f, 0.1f, 1); 
} 

smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)) as GameObject;代碼將實例化遊戲對象分配給名爲smokeinstance參數中的局部變量。它不會將它分配給傳入它的GameObjects,例如newSmoke1,newSmoke2newSmoke3

newinstance(smokeposition1, newSmoke1); 
newinstance(smokeposition2, newSmoke2); 
newinstance(smokeposition3, newSmoke3); 

SOLUTION

你有2個解決方案

。使用的refout關鍵字。在這種情況下,我們只會使用ref關鍵字。

void newinstance(Vector3 smokeposition, ref GameObject smokeinstance) 
{ 
...... 
} 

然後在調用它時也使用ref

newinstance(smokeposition1, ref newSmoke1); 
newinstance(smokeposition2, ref newSmoke2); 
newinstance(smokeposition3, ref newSmoke3); 

。使newinstance函數返回GameObject代替void

GameObject newinstance(Vector3 smokeposition) 
{ 
    GameObject smokeinstance = Instantiate(myPrefab, smokeposition, Quaternion.Euler(-90, 0, 0)); 
} 

然後調用它:

newSmoke1 = newinstance(smokeposition1); 
newSmoke2 = newinstance(smokeposition2); 
newSmoke3 = newinstance(smokeposition3); 

的這個任何一個應該解決您的問題。

+0

你是男人!當場就好了。我使用瞭解決方案1,併爲toggle1,toggle2和toggle 3做了同樣的工作,它工作正常!我明白什麼是裁判!非常感謝! –

+0

不客氣。這只是C#中棘手的部分之一。 – Programmer