2017-04-20 69 views
0

我在另一個對象附近創建了GameObject。函數「Instantiate」重複自身(進入循環),這就是問題所在。我需要它被稱爲一次。我試過: 1.調用函數按下按鈕(GetKey)。此功能仍然可以創建3-4個對象。 2.在Update和FixedUpdate中添加代碼。而且它還創造了幾個對象。 :(函數進入循環(重複自己)而不是僅僅工作一次

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class table : MonoBehaviour { 

public GameObject obj; 
public Renderer rend; 

void Start() { 

    rend = GetComponent<Renderer>(); 

    Vector3 center = rend.bounds.center; 
    Vector3 pos = center + new Vector3 (12,0,0); 

    Instantiate (obj, pos, Quaternion.identity); 

} 

} 

okay

Not okay

回答

2

您調用的函數Instantiate (obj, pos, Quaternion.identity);Start方法。您的腳本連接到您實例化GameObject,這意味着它會在每次產生GameObject時,請調用Start方法,這將使用新腳本創建一個新腳本並調用該腳本的Start方法。

可能的解決方案:
1.招行Instantiate (obj, pos, Quaternion.identity);的方法/函數調用該函數根據要求
2.從Prefab刪除腳本(在你的情況下,它被稱爲OBJ在腳本中)。

相關問題