Start method activating multiple times when StartCoroutine() is in it

I’m new to Unity and for some reason every time the WaitForSeconds() in a coroutine ends the start method gets called again. This happens forever.

IEnumerator ChunkWait()
{
    yield return new WaitForSeconds(1f);
    ChunkLoad();
}

void Start()
{
    chunk = gameObject;
    StartCoroutine(ChunkWait());
    Debug.Log("This is activating every time the WaitForSeconds() is over");
}

Also, when I put the WaitForSeconds() below the ChunkLoad(); it starts getting spammed very quickly
If anybody knows why any of this happens, help would be greatly appreciated.

I guess that your ChunkLoad method creates new chunks, right? Is it possible that this script is on every chunk?

If that’s the case, note that you don’t see the debug log when the coroutine of this object has finished. The debug.Log inside Start is executed immediately after StartCoroutine, all within the same frame it was started. As soon as the coroutine hits the first yield, StartCoroutine would return and you would finish the Start method and print that log message. After your 1 second delay your coroutine would call ChunkLoad which probably creates a new object that has this script attached.

As a result you get a new object every time. Try modifying the Log to look like this:

Debug.Log("New Chunk: " + gameObject.name, gameObject);

Note that we now print the name of the object this script is on as well as passing a context object to the log message. That means when you click on the message in the console, Unity will ping / highlight that specific object in the hierarchy. So clear your console and let your game run for a few seconds and then press pause. Now you can examine your log messages one by one to see which object produced them.

Start only runs once for a single instance and is never called again unless you call it manually again (which you should never do)

.

Of course, when you call ChunkLoad before your WaitForSeconds call it means you load that new object essentially right inside the Start method without any delay. Since the Start of the new object is run the next frame and not right at the time the object is created, you would get a 1 frame delay between your calls. So you would create a new chunk every frame in that case rather than about 1 every second.

We don’t know what you want to do and we only know what you have shown us so far. So we can not give you any advice or recommendation what you should do. That’s something you have to figure out yourself. Though the Debug.Log call should help you understanding your current setup better.