【发布时间】:2013-05-01 19:47:55
【问题描述】:
我遇到了这个我似乎无法完全理解如何解决的小问题,我已经尝试过无数次更改我的代码但我没有得到任何结果:(
我正在以编程方式创建游戏对象,效果很好,但问题是游戏每帧创建一次对象(不完全是我想要的)!所以我设置了 10 秒的时间延迟,但它似乎无法正常工作。
public Vector3 spawnLocation;
public GameObject myCube;
// Use this for initialization
void Start () {
if (myCube.renderer.enabled == false) {
Debug.Log("myCube not rendered");
myCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
}
if (myCube == null) {
Debug.Log("myCube not set");
myCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
}
}
// Update is called once per frame
void Update () {
StartCoroutine(Delay());
Destroy(myCube, 5);
CreateCube();
}
void CreateCube() {
spawnLocation = new Vector3(24, 17, -28);
StartCoroutine(Delay());
Instantiate(myCube, spawnLocation, Quaternion.identity);
}
IEnumerator Delay(){
yield return new WaitForSeconds(10);
}
对象在每一帧都无休止地出现-_-
谁能帮我指出正确的方向。 有没有更好的方法来实现这一点?
【问题讨论】:
-
每次游戏循环时,您都会调用
CreateCube()。贴在Start()。 -
@David 我试过了,但是如果我将
CreateCube()放在Start()中,它只会创建一次游戏对象!我想无休止地创建myCube,但有一个时间间隔。 -
// 每帧都会调用 Update()。如果您是新手,请避免使用它,直到您了解如何实现事件模式,否则您将陷入噩梦。