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. Dismiss Notice

Trouble with Tag not being defined.

Discussion in 'Scripting' started by Raoul1234, Aug 29, 2016.

  1. Raoul1234

    Raoul1234

    Joined:
    Aug 29, 2016
    Posts:
    2
    Hi,

    in the following code:

    Code (CSharp):
    1. public string enemyTag = "Enemy";
    2.  
    3. void UpdateTarget()
    4.     {
    5.         GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
    6.         float shortestDistance = Mathf.Infinity;
    7.         GameObject nearestEnemy = null;
    8.         foreach (GameObject enemy in enemies)
    9.         {
    10.             float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
    11.             if (distanceToEnemy < shortestDistance)
    12.             {
    13.                 shortestDistance = distanceToEnemy;
    14.                 nearestEnemy = enemy;
    15.             }
    16.         }
    17.  
    18.         if (nearestEnemy != null && shortestDistance <= range)
    19.         {
    20.             target = nearestEnemy.transform;
    21.         }
    22.         else
    23.         {
    24.             target = null;
    25.         }
    26.  
    27.     }
    I would receive an error on line 5: UnityException: Tag: Enemy is not defined.

    How ever by creating a public string called enemyTag = "Enemy"; where Enemy is an object in the game. Calling it from GameObject.FindGameObjectsWithTag(enemyTag); seems to not work.

    While the same principe works from different scripts running in the game.


    (This script is from a tower defense tutorial on youtube. Im trying to learn basics and best systems for these types of games and am manualy typing the code provided by the video. I checked for typo's and errors by comparing the video's project and mine. But no differences.)

    P.S. The code is slimmed down theres allot more i can post here but i will do if required for extra clearance.


    Thanks in Advance

    Raoul
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Did you create a tag called "Enemy"?

    In your scene, click on pretty much anything. In the inspector, you'll see tag. Use the drop down and check that you have an Enemy tag.
     
    rajput0 likes this.
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You have that variable as public, so it's possible it's empty in the inspector, which will overwrite your initialized value of "Enemy" potentially. So double check you have an Enemy tag created, and double check your inspector value.
     
  4. Raoul1234

    Raoul1234

    Joined:
    Aug 29, 2016
    Posts:
    2
    I feel silly. @Brathnann i forgot... Thats like basics if you point to a tag then you shouldve created a tag...
     
    LiterallyJeff likes this.
  5. rajput0

    rajput0

    Joined:
    Jan 29, 2021
    Posts:
    1
    Thank you!