I have been following a tutorial and have taken what I’ve learned so far and put a lot of it to work. I have a trigger that when the Player walks through it an enemy NPC will spawn. It works just fine but not matter what it seems to spawn more that 1. The script lets me set a valuse and I set it to spawn only one but I feel like I must be hitting the trigger several times with each leg or sometimes the NPC’s will walk over it and they Spawn more of themselves. Evil guys ![]()
Anyway here is the script I’m using. Is there a good way to mod it to alway only spawn just one or is there something different I should do with my trigger? I have tried to make it as thin as possible in hopes that the Player passes through fast enough to only trigger it once but no joy. Much thanks to all you brillant people who help around here.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Chapter1
{
public class Spawner : MonoBehaviour
{
public GameObject objectToSpawn;
public int numberOfEnemies;
private float spawnRadius = 5;
private Vector3 spawnPosition;
private GameManager_EventMaster eventMasterScript;
void OnEnable()
{
SetInitialReferences();
eventMasterScript.myGeneralEvent += SpawnObject;
}
void OnDisable()
{
eventMasterScript.myGeneralEvent -= SpawnObject;
}
void SpawnObject()
{
for(int i =0;i<numberOfEnemies;i++)
{
spawnPosition = transform.position + Random.insideUnitSphere * spawnRadius;
Instantiate(objectToSpawn, spawnPosition, Quaternion.identity);
}
}
void SetInitialReferences()
{
eventMasterScript = GameObject.Find("GameManager").GetComponent <GameManager_EventMaster>();
}
}
}