Heroes_of_Hiis/Assets/Project Files/Scripts/JonasB/SlimeSpawner.cs

37 lines
1.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlimeSpawner : MonoBehaviour
{
[SerializeField]
private List<GameObject> slimeTypes;
[SerializeField]
private List<Transform> spawnLocations;
[SerializeField]
private int maxAmount = 5;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("SpawnSlime", 5, 5);
}
public void SpawnSlime()
{
int alive = GameObject.FindGameObjectsWithTag("Slime").Length;
if (alive == maxAmount)
{
// Max amount of slimes reached.
Debug.LogWarning("Maximum reached");
}
else
{
float slimeRandom = Random.Range(0, slimeTypes.Count);
float spawnRandom = Random.Range(0, spawnLocations.Count);
Instantiate(slimeTypes[Mathf.RoundToInt(slimeRandom)], spawnLocations[Mathf.RoundToInt(spawnRandom)].position, Quaternion.Euler(0f, Random.Range(0f, 360f), 0f));
}
}
}