Search Unity

Question EnemySpawner not working as intended

Discussion in 'Scripting' started by Mynamebiber, Feb 19, 2024.

  1. Mynamebiber

    Mynamebiber

    Joined:
    Feb 2, 2024
    Posts:
    2
    Hello! So, I am trying to create an spawner for the monsters to spawn at. The game starts with a slime and a goblin on the field, and I want more of those to spawn in over time. However, when I use the script I have right now, my slime becomes super fast and just bounces around everywhere. Then, when I kill that slime, the next slime spawns in (but without the healthbar until I hit it). But THEN, when another slime tries to spawn in, the previously spawned slime begins moving super fast and the new slime doesn't spawn. Any ideas? Here is the respawn code:


    using UnityEngine;

    public class GooberSpawner : MonoBehaviour
    {
    public GameObject gooberPrefab; // Prefab for the Goober enemy
    public Transform spawnPoint; // Spawn point for the enemies

    // Spawn interval in seconds
    public float spawnInterval = 5f;

    // Start is called before the first frame update
    void Start()
    {
    // Start spawning Goobers
    InvokeRepeating("SpawnGoober", 0f, spawnInterval);
    }

    // Method to spawn a Goober at the spawn point
    void SpawnGoober()
    {
    // Check if the spawn point and Goober prefab are assigned
    if (spawnPoint != null && gooberPrefab != null)
    {
    // Instantiate a Goober at the spawn point position with default rotation
    GameObject goober = Instantiate(gooberPrefab, spawnPoint.position, Quaternion.identity);
    goober.SetActive(true); // Ensure Goober is active
    }
    else
    {
    Debug.LogWarning("Spawn point or Goober prefab is not assigned in the inspector.");
    }
    }
    }
     
  2. Mynamebiber

    Mynamebiber

    Joined:
    Feb 2, 2024
    Posts:
    2
    Actually, I think this might stem from my other scripts. I duplicated the slime (goober) script to have 2 slimes appear on the screen to start, and it recreates the same bug as above. Is there a rule of thumb I should follow when creating scripts for monsters to avoid this issue? I am not getting any errors in the console, but this is definitely not working as intended haha
     
  3. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    227
    From your description, it seems like this code is present on every object representing an appearing enemy. In other words, each enemy serves as a spawner.