2016-10-04 72 views
2
using UnityEngine; 
using System.Collections; 

public class instantiatescript : MonoBehaviour { 

public GameObject house; 

// Use this for initialization 
void Start() { 
    house = GameObject.FindGameObjectWithTag ("house"); 
    CreatePrefab(); 
} 

// Update is called once per frame 
void Update() { 


} 


void CreatePrefab() 
{ 



    for (int i = 0; i < 10; i++) 
     Instantiate(house, new Vector3(i * 0f, 0f, 2.0f), Quaternion.identity); 


} 
} 

我需要沿z direction.but無限生成的遊戲對象房子現在這段代碼實際上什麼也不做,任何一個可以提供無限代對象的引用無限生成的遊戲對象

回答

2

邏輯有點正確,但是:

  • house不需要用實例化的GameObject進行初始化。只需保留聲明並通過檢查員指定一個預製變量;
  • forfor週期應該考慮多個x值並在每次迭代時生成一個房子。但是,你期望通過將任意i乘以0來期待什麼?座標將始終保持不變。也許你想補充?
+0

yaa我需要添加新的克隆對象,所以它看起來像很多房子在遊戲中......其實我正在做一個跑步遊戲像寺廟運行 –

+0

行,理解。所以,作爲第一次嘗試,請遵循第二點(即用「+」替換「*」) –

+1

我想說你應該把這代人與玩家運動結合起來,你不希望提前產生1000個遊戲對象相反,你想要在屏幕的右側(和左側)產生(並摧毀或移動)它們,例如你可以根據播放器的運行速度來做到這一點 –