Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question CS0246

Discussion in 'Getting Started' started by notjake85, Apr 18, 2024.

  1. notjake85

    notjake85

    Joined:
    Feb 20, 2024
    Posts:
    44
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemySpawnController : MonoBehaviour
    6. {
    7.     public int initialEnemiesPerWave = 5;
    8.     public int currentEnemyPerWave;
    9.  
    10.     //delay between enemies in a wave
    11.     public float spawnDelay = 0.5f;
    12.  
    13.     public int currentWave = 0;
    14.     //delay between waves
    15.     public float waveCoolDown = 12f;
    16.  
    17.     public bool inCoolDown;
    18.     //we only use this for testing
    19.     public float cooldownCounter = 0;
    20.  
    21.     public List<Enemy> currentEnemiesAlive;
    22.    
    23.     public GameObject enemyPrefab;
    24.  
    25.     public void Start()
    26.     {
    27.         currentEnemyPerWave = initialEnemiesPerWave;
    28.  
    29.         StartNextWave();
    30.     }
    31.  
    32.     private void StartNextWave()
    33.     {
    34.         currentEnemiesAlive.Clear();
    35.  
    36.         currentWave++;
    37.        
    38.         StartCoroutine(SpawWave());
    39.     }
    40.  
    41.     private IEnumerator SpawnWave()
    42.     {
    43.         for (int i =0; i < currentEnemyPerWave; i++)
    44.         {
    45.             //generate a random offset within a range
    46.             Vector3 spawnOffset = new Vector3(Random.Range(-1f,1f), 0f, Random.Range(-1f, 1f));
    47.             Vector3 spawnDelayPosition = transform.position + spawnOffset;
    48.  
    49.             //instantiate the enemy
    50.             var enemy = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);
    51.  
    52.             //get enemy Script
    53.             EnemyAI enemyScript = enemy.GetComponent<EnemyAI>();
    54.  
    55.             //track enemy
    56.             currentEnemiesAlive.Add(enemyScript);
    57.  
    58.             yield return new WaitForSeconds(spawnDelay);
    59.  
    60.  
    61.         }
    62.     }
    63.  
    64.     private void Update()
    65.     {
    66.         List<Enemy> enemyToRemove = new List<Enemy>();
    67.         foreach (Enemy enemy in currentEnemiesAlive)
    68.         {
    69.             if (enemy.isDead)
    70.             {
    71.                 enemyToRemove.Add(enemy);
    72.             }
    73.         }
    74.  
    75.         // actually remove all dead enemies
    76.         foreach (Enemy enemy in enemyToRemove)
    77.         {
    78.             currentEnemiesAlive.Remove(enemy);
    79.         }
    80.  
    81.         enemyToRemove.Clear();
    82.  
    83.         //start cooldown if all enemies are dead
    84.         if (currentEnemiesAlive.Count == 0 && inCoolDown == false)
    85.         {
    86.             //start cooldown for next wave
    87.             StartCoroutine(waveCoolDown());
    88.         }
    89.  
    90.         //run cooldown counter
    91.         if (inCoolDown)
    92.         {
    93.             cooldownCounter -=Time.deltaTime;
    94.         }
    95.         else
    96.         {
    97.             cooldownCounter = waveCoolDown;
    98.         }
    99.     }
    100.  
    101.     private IEnumerator WaveCoolDown()
    102.     {
    103.         inCoolDown = true;
    104.  
    105.         yield return new WaitForSeconds(waveCoolDown);
    106.  
    107.         inCoolDown = false;
    108.  
    109.         currentEnemyPerWave *= 2;
    110.         StartNextWave();
    111.     }
    112.  
    113. }
    114.  
    Is there anything wrong with this code? I have been stuck :|

    Assets\Scripts\EnemySpawnController.cs(21,17): error CS0246: The type or namespace name 'Enemy' could not be found (are you missing a using directive or an assembly reference?)
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,544
    It's saying at line 21 starting with character 17 it doesn't recognize
    Enemy
    . Is there a class or struct with that name in your project?
     
  3. notjake85

    notjake85

    Joined:
    Feb 20, 2024
    Posts:
    44
    No class.
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,544
    You need either a class or struct. The name you put in the
    <
    and
    >
    is the name of a class or struct that will be used as the type of the
    List
    .