Files
Gameshit/GOAP/Assets/Scrips/Spawner.cs
2025-05-31 15:54:23 +02:00

43 lines
1.2 KiB
C#

using System;
using UnityEngine;
public class Spawner : MonoBehaviour
{
[SerializeField] private GameObject _spawnTemplate = null;
[SerializeField] private int _spawnCount = 1;
[SerializeField] private float _spawnInterval = 1.0f;
private float _nextSpawnTime = 0.0f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
_nextSpawnTime = Time.time + _spawnInterval;
}
private void Update()
{
if (Time.time >= _nextSpawnTime)
{
for (int i = 0; i < _spawnCount; i++)
{
Spawn();
}
_nextSpawnTime = Time.time + _spawnInterval;
}
}
public GameObject Spawn()
{
if (_spawnTemplate == null)
{
Debug.LogError("Spawn template is not assigned in the Spawner component.");
return null;
}
GameObject spawnedObject = Instantiate(_spawnTemplate, transform.position, transform.rotation);
spawnedObject.transform.SetParent(transform); // Optional: Set parent to keep hierarchy organized
return spawnedObject;
}
}