Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Spawn Rarity System

Hi

So i have a Spawner which spawns a SpawnPattern Prefab which that spawns the Obsticles and Enemies.

And i have a rarity system which defines the spawn chances of different Obsticles.

and this is the code for that:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


[System.Serializable]

public class ItemToSpawn

{

  public GameObject item;

  public float spawnRate;

  [HideInInspector] public float minSpawnProb, maxSpawnProb;

}

public class SpawnPoint : MonoBehaviour

{

  public ItemToSpawn[] itemToSpawm;

  private void Start()

  {

    for (int i = 0; i < itemToSpawm.Length; i++)

    {

      if (i == 0)

      {

        itemToSpawm[i].minSpawnProb = 0;

        itemToSpawm[i].maxSpawnProb = itemToSpawm[i].spawnRate - 1;

      }

      else

      {

        itemToSpawm[i].minSpawnProb = itemToSpawm[i - 1].minSpawnProb + 1;

        itemToSpawm[i].maxSpawnProb = itemToSpawm[i].minSpawnProb + itemToSpawm[i].spawnRate - 1;

      }

    }

    Spawnner();

  }


  void Spawnner()

  {

    float randomNum = Random.Range(0, 100);

    for (int i = 0; i < itemToSpawm.Length; i++)

    {

      if (randomNum >= itemToSpawm[i].minSpawnProb && randomNum <= itemToSpawm[i].maxSpawnProb)

      {

        Instantiate(itemToSpawm[i].item, transform.position, Quaternion.identity);

        break;

      }

    }

  }

}

____________________________________________________

The problem is that sometimes this rarity system doesnt spawn anything. and i dont know what to do

how can i fix it?

Sign In or Register to comment.