Home > Software design >  Loads of objects being spawned in a short period after setting a random range
Loads of objects being spawned in a short period after setting a random range

Time:12-23

new to unity and C# and currently going through the create with code course. For the Prototype 2 exercise im trying to set it so that dogs are spawned between 2 and 5 seconds, each time varying slightly with randomness. However what happens at the moment is that they are spawned in random intervals, however there is then a huge increase in the amount of dogs spawned and it quickly fills up the scene and uses alot of processing power:

Large amount of objects spawned in short period

My SpawnManager script is as follows:

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

public class SpawnManager : MonoBehaviour
{
    public GameObject[] dogPrefabs;

    private float spawnRangeX = 10;
    private float spawnPosZ = 20;
    private float startDelay = 2;
    private float startspawnDelay = 1.5f;
    public float RandomSpawnBeg = 2; //* Time.deltaTime;
    public float RandomSpawnEnd = 5; //* Time.deltaTime;
    public float RandomSpawn = Random.Range(RandomSpawnBeg, RandomSpawnEnd);
    //public float boneDelay = 2.0f;

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("SpawnRandomAnimal", startDelay, RandomSpawn); //calls SpawnRandomAnimals method, after 2 seconds, every 1.5 seconds.
    }

    // Update is called once per frame
    void Update()
    {
       
    }
    void SpawnRandomAnimal()
    {
        
        int animalIndex = Random.Range(0, dogPrefabs.Length);
        Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ); //creating a Vector3 variable which sets a random value for the x axis between the spawnRange vars, 0 on the y axis, and the spawnPosZ var for the z axis.

        Instantiate(dogPrefabs[animalIndex], spawnPos, dogPrefabs[animalIndex].transform.rotation); // spawn an animal from the index with an updated vector 3 position and rotation
        
        //InvokeRepeating("SpawnRandomAnimal", startDelay, RandomSpawn);
        Debug.Log("Spawn time is "   RandomSpawn);
    }
}

Im a bit lost as to what could be causing this onslaught of dogs. I though that it may be telling it to do it every 2 - 5 frames as oppose to seconds, however when i * RandomSpawnBeg and RandomSpawnEnd by time.deltatime its still performing the same way.

Im really enjoying learning C# and unity but getting a bit stuck on how time works within it, if anyone can shed some light on this for me i would really appreciate it!

Thanks

CodePudding user response:

first of all the InvokeRepeating function

public void InvokeRepeating(string methodName, float time, float repeatRate);

Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds. example InvokeRepeating("LaunchProjectile", 2.0f, 0.3f); it means that i'll create a projectile after 2 seconds from the Start frame and each 0.3 second i'll call this LaunchProjectile again . This is for the invoke repeating now we attack your problem : Start() the Start() function is called in Unity once and only once at the first frame before calling the Update(). So basically you are invoking repeatedly with the same amount of frame rate each time because the randomizing happens only once. To fix this just change where you call the randomizing function to this :

void Start()
{
    InvokeRepeating("SpawnRandomAnimal", startDelay,Random.Range(RandomSpawnBeg, RandomSpawnEnd)); 
}

Now test this my friend and I hope your journey in Unity is full of joy. I advice you to review this documentation it will help you out in many ways Order of execution

  • Related