T
T
TupaDev2020-12-26 00:29:18
Unity
TupaDev, 2020-12-26 00:29:18

A huge number of objects spawn, what should I do?

The code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawn : MonoBehaviour
{
    public GameObject Smile;

    private void Start()
    {
      StartCoroutine(SummonSmile());
    }
    IEnumerator SummonSmile()
    {
      while (true)
      {
        SpawnSmile();
        yield return new WaitForSeconds(1);
      }
  }

  private void SpawnSmile()
  {
    Instantiate(Smile, new Vector2(Random.Range(-2, 2), 6f), Quaternion.identity);
  }	
}

The problem itself:
A huge number of objects spawn every millisecond, and it turns out -pc
I would attach a screenshot but I can’t because pc RIP

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GFX Data, 2020-12-26
@TupaDev

private void Start()
    {
      InvokeRepeating("SpawnSmile", 0f, 1f);
    }

P
piffo, 2020-12-26
@piffo

while (true)
      {
        SpawnSmile();
        yield return new WaitForSeconds(1);
      }

endless cycle

W
WeBBeW, 2020-12-26
@WeBBeW

So that a lot of objects do not spawn, then you can make sure that if there are more objects created than necessary, then they stop being created.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question