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

Create with code lesson 4.4 - The type or namespace name 'Enemy' could not be found

Discussion in 'Community Learning & Teaching' started by chaffoilofolaz, Jun 22, 2020.

  1. chaffoilofolaz

    chaffoilofolaz

    Joined:
    Sep 27, 2018
    Posts:
    2
    Hello
    I'm following the tutorial Create With code
    they are very instructive but I'm having an issue with the 4.4 lesson
    the following code is not compiling and return "The type or namespace name 'Enemy' could not be found" :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnBalls : MonoBehaviour
    6. {
    7.     public GameObject enemy;
    8.     public float spawnRange;
    9.     public bool hasPowerup = false;
    10.     public int enemyCount;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         SpawnWaves(1);
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         enemyCount = FindObjectsOfType<Enemy>().Length;
    21.        
    22.         if (enemyCount == 0)
    23.         {
    24.             SpawnWaves(22);
    25.         }
    26.     }
    27.    
    28.     private void SpawnWaves (int enemyToSpawn)
    29.     {
    30.         for(int i  = 0 ; i < enemyToSpawn ; i++)
    31.         {
    32.             Spawn();
    33.            
    34.         }
    35.     }
    36.    
    37.     void Spawn()
    38.     {
    39.         float x = Random.Range(-spawnRange, spawnRange);
    40.         float y = Random.Range(-spawnRange, spawnRange);
    41.         Instantiate(enemy, new Vector3(x, 0 , y), enemy.transform.rotation);
    42.     }
    43.    
    44.     private void OnTriggerEnter(Collider other)
    45.     {
    46.         if (other.CompareTag("PowerUp"))
    47.         {
    48.             hasPowerup = true;
    49.             Destroy(other.gameObject);
    50.        
    51.         }
    52.     }
    53. }
    54.  
    I've try to change "Enemy" by some other prefabs and it return me the same error.
    could you please help me ?
     
    boraevren2013 likes this.
  2. chaffoilofolaz

    chaffoilofolaz

    Joined:
    Sep 27, 2018
    Posts:
    2
    Ok I found a solution
    FindObjectsOfType<Enemy>()
    could not work since Enemy is not a type but a tag or a prefab name
    so I did this and it's working


    Code (CSharp):
    1. GameObject[] obj = GameObject.FindGameObjectsWithTag("Enemy");
    2.         enemyCount = obj.Length;
    may be the tutorial need a small update ?
     
  3. Arkemex

    Arkemex

    Joined:
    Jan 14, 2021
    Posts:
    1
    Thank you for this! Kind of sad where in 2 years they haven't updated it. I will send them an email to fix this issue.
     
  4. rn92030901

    rn92030901

    Joined:
    Nov 26, 2022
    Posts:
    1
    you miss a "s" in ur code.
    it's.............Find"Objects"OfType
    enemyCount = FindObjectsOfType<Enemy>().Length;
     
    jfedd1999, SamB05, RafaelSaSo and 2 others like this.
  5. starwalkerluke_lu

    starwalkerluke_lu

    Joined:
    May 5, 2023
    Posts:
    2
    That's the case