Domanda

I've come across this little issue which I can't seem to fully understand how to solve, I've tried changing my codes countless times but I haven't gotten anywhere :(

I am creating game objects programmatically which works fine, but the issue is that the game creates the objects once per frame (not exactly what I want)! So I've put in a time delay of 10 seconds but it doesn't seem to work properly.

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);
}

The objects appear endlessly at every frame -_-

Could anyone please help point me in the right direction where am going off. And is there a better way to achieve this?

È stato utile?

Soluzione

I've put in a time delay of 10 seconds but it doesn't seem to work properly.

Delay works but you are still Instantiating the cubes inside Update. If you want to instantiate a Cube every 10 seconds you cand do the following:

IEnumerator DelayCreateCube(float delay){

    while(true)
    {
      yield return new WaitForSeconds(10);
      Instantiate(myCube, spawnLocation, Quaternion.identity);
    }

}

public void Start()
{
  StartCoroutine(DelayCreateCube());
}

Coroutines are executed by the engine at every frame (well, it depends on the coroutine) generally (at least in your case) immediately after all the Update methods of all GameObjects have been called.

When you use StartCoroutine, it simply schedules the coroutine to be executed later, so it doesn't block the execution of Update method:

StartCoroutine(Delay());
Destroy(myCube, 5);
CreateCube();

in the code above Delay will be executed only after Update method has returned, so you were not actually delaying the execution of Destroy and CreateCube methods.

For a more detailed description of coroutines, have a look at this article.


EDIT

Just an additional consideration. I don't know exactly what you are trying to do, but it seems that you want to create and destroy a primitive at always the same location. If you haven't a good reason to do that, you can simply enable/disable the object instead of creating a new one and destroy it again (for performances).

Altri suggerimenti

Obviously other people have pointed out the fact that you are creating a cube every time Update() is called, which is once every game loop. Assuming you only want to create objects at an even interval of time, e.g. once every X seconds, you should consider using either a Coroutine or an InvokeRepeating. Coroutine solution has already been addressed, InvokeRepeating is given below:

void Start()
{
  InvokeRepeating("CreateCube", 1.0f, 1.0f);
}

void CreateCube()
{
    // create your cube here
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top