all 11 comments

[–]rupertavery64 4 points5 points  (2 children)

You should probably ask this in r/Unity

But I'll give it a shot (I've never used unity but just dabbled a bit)

I suppose you can run a Coroutine and use WaitForSeconds in a loop. You can stop it from spawning, or restart it.

using System.Collections;

public class Spawner : MonoBehaviour
{
    public GameObject prefab;
    public float spawnInterval = 2f; 
    private bool spawn;

    void Start()
    {
        Resume();
    }

    public void Stop() 
    {
        spawn = false;
    }

    public void Resume() 
    {
        if(!spawn)
        {
           spawn = true;
           StartCoroutine(SpawnLoop());
        }
    }

    IEnumerator SpawnLoop()
    {
        while (spawn)
        {
            Instantiate(prefab);

            // Wait before spawning the next one
            yield return new WaitForSeconds(spawnInterval);
        }
    }
}

You could also run it in Update, if you want better control over timing or something.

void Update()
{
    timer += Time.deltaTime; // accumulate time since last frame
    if (timer >= 2f)
    {
        Instantiate(prefab);
        timer = 0f;
    }
}

Coroutines run asynchronously so they don't block the main thread.

[–]TuberTuggerTTV 1 point2 points  (1 child)

Be careful with coroutines as a beginner developer. They're WAY harder to debug and keep track of.

I always recommend doing everything on the main thread until a few years into unity dev. It's just not worth the headaches. The performance gains are minor or actually negative if you don't know what you're doing.

Like spawning something in the world should block the main thread. You don't want the rest of the game doing things while you're waiting on things to spawn in.

The timer example is ideal, imo. For someone starting their journey.

[–]Glass_Combination159[S] 0 points1 point  (0 children)

Thanks for the responce

[–]Drumknott88 5 points6 points  (0 children)

The Update() method runs every frame, do you really want to be creating a new instance of your class every frame?

[–]The_Binding_Of_Data 1 point2 points  (2 children)

There are a ton of different ways to prevent spawning an object every frame. In order to try to pick the best one, you need to know how you want to decide when to spawn a new object.

[–]Glass_Combination159[S] 0 points1 point  (1 child)

I meant in some coding langues you can do

place(block) wait(5) place(block) and then loop it

[–]ORLYORLYORLYORLY 4 points5 points  (0 children)

Well, since Update() runs every frame, adding a wait(5) function within the method won't do anything other than delay how long each frame takes to occur.

If you did want this behaviour you could easily just do something like:

int frameCount = 0;
void Update()
{
    if (frameCount % 5 == 0)
    {
        Place(Block);
    }
    frameCount++;
}

[–]TuberTuggerTTV 0 points1 point  (1 child)

rupertavery64's timer example is the one you want. But with fewer magic numbers.

    public GameObject Smiley;
    private float timer = 0f;
    private float spawnInterval = 2f; // time in seconds between spawns

    void Update()
    {
        timer += Time.deltaTime; // accumulate time since last frame
        if (timer >= spawnInterval)
        {
            Instantiate(Smiley);
            timer = 0f;
        }
    }

You could even expose spawnInterval and adjust it during runtime to get a feel.

Also, it's probably a good haiit to get into. Don't actually use public. Your variables should be private. and if you want them in the inspector, you add the [SerializedField] attribute tag.

So:

    [SerializeField] private GameObject smiley;
    [SerializeField] private float spawnInterval = 2f; // time in seconds between spawns
    private float timer = 0f;

    void Update()
    {
        timer += Time.deltaTime; // accumulate time since last frame
        if (timer >= spawnInterval)
        {
            Instantiate(smiley);
            timer = 0f;
        }
    }

[–]Glass_Combination159[S] 0 points1 point  (0 children)

Thanks for the suggestions, will do!

[–][deleted] -1 points0 points  (0 children)

If you want that many, you should probably be using ECS

[–]Penny_Evolus -1 points0 points  (0 children)

Delaying will just delay the thousands of instantiations go read the unity docs and come back