Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with an enemy spawning bug

Discussion in 'Scripting' started by NickEberhart, Jul 6, 2015.

  1. NickEberhart

    NickEberhart

    Joined:
    Jul 6, 2015
    Posts:
    6
    I am making a 2D tank battle game that spawns enemies forever. I put the enemySpawning script onto an empty game object at the origin and give it the enemy prefab. Then, when I play it the enemies are spawning (I can see them in the Inspector and I take damage from nothing. So could someone look at my code and see why it is spawning invisible enemies? Any help is greatly appreciated!
     

    Attached Files:

  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    If you need a delay between something happening such as your spawning enemies then I suggest you use either a coroutine or invoke repeating, both will allow you to create a delay in a much cleaner way than you are currently doing.

    Code (csharp):
    1.  
    2. public class EnemySpawner : MonoBehaviour {
    3.  
    4.     public GameObject enemyPrefab;
    5.  
    6.     float spawnDistance = 12f;
    7.  
    8.     float enemyRate = 5;
    9.  
    10.     void Start()
    11.     {
    12.         InvokeRepeating("SpawnEnemy", 0, enemyRate);
    13.     }
    14.    
    15.     void SpawnEnemy()
    16.     {
    17.         Vector3 offset = Random.onUnitSphere;
    18.         offset.z = 0;
    19.         offset = offset.normalized * spawnDistance;
    20.         Instantiate(enemyPrefab, transform.position + offset, Quaternion.identity);
    21.     }
    22. }
     
    LeftyRighty likes this.
  3. NickEberhart

    NickEberhart

    Joined:
    Jul 6, 2015
    Posts:
    6
    Thank you, will this also fix the invisible spawning?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    No I cant see anything that would cause invisible enemies. What do your prefabs look like?
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Are they spawning below your floor/ground? I had that issue, it wasn't until I moved the view in the editor that i saw them spawning & moving around below the ground level
     
  6. NickEberhart

    NickEberhart

    Joined:
    Jul 6, 2015
    Posts:
    6
    That might be it! Would they be able to still to damage to the player?
     
  7. NickEberhart

    NickEberhart

    Joined:
    Jul 6, 2015
    Posts:
    6
    Also when I start the game it give this error...
    UnassignedReferenceException: The variable enemyPrefab of EnemySpawner has not been assigned.
    You probably need to assign the enemyPrefab variable of the EnemySpawner script in the inspector.
    I assigned the prefab which is what I thought the error was talking about, am I misunderstanding it?
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    1) depending how the enemy affects the player it could. Run the game without maximising, when you see the enemy spawn pause the game. Click on the enemy in the inspector then put the mouse over the editor part & hit f. It will take you to the enemy then you can work out where it is.

    2) that error is as you say. Check all your objects to see if the script is assigned to more than one thing. Assigning a prefab to a prefab works for the scene but isn't retained on the actual prefab.
     
    NickEberhart likes this.
  9. NickEberhart

    NickEberhart

    Joined:
    Jul 6, 2015
    Posts:
    6
    I found that the enemies were spawning behind the camera so I just moved the camera back a bit. Thank you very much!
     
    outis_52 likes this.
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You're welcome, good luck :)