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

I don't know how to check the tags of the gameobjects in a list

Discussion in 'Scripting' started by r1a2f3, Mar 19, 2021.

  1. r1a2f3

    r1a2f3

    Joined:
    Jul 2, 2020
    Posts:
    3
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Code : MonoBehaviour
    {
    public List<GameObject> slots;

    private void Update()
    {
    foreach (GameObject i in slots)
    {
    //Check their tags
    if (i.CompareTag("Enemy"))
    {
    //Execute whatever instructions are here
    Debug.Log("The code is working");
    }
    }
    }
    }


    But the message don't appears in the console, there are other ways to check the gameobjects tags in a list?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Use code tags

    Have you assigned the "slots" objects in the inspector? Can you show a screenshot of this object's inspector?
     
  3. r1a2f3

    r1a2f3

    Joined:
    Jul 2, 2020
    Posts:
    3
    I changed the name of the script "Code" to "Puzzle_inventory" in the Visual Studio and in the Unity.
     

    Attached Files:

  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    OK, and you're sure these GameObjects have that tag assigned? Spelling and capitalization must be exact.

    Try adding this log (outside your if statement) to get more info:
    Code (csharp):
    1. Debug.Log($"This object's tag is {i.tag}");