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

Interact with NPC using Tag

Discussion in 'Scripting' started by Europa2019, Jun 1, 2019.

  1. Europa2019

    Europa2019

    Joined:
    May 23, 2019
    Posts:
    5
    Hi guys.

    I have a NPC named "Douglas@Idle" which I have given the tag "NPC". I want in my script to check if the tag is present, then play audio.

    When I enter "Douglas@Idle" collision zone the trigger is working, but I get "TAG=Undefined".


    Full screen image: https://ibb.co/vP3Lg3r

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DouglasPoliceStation : MonoBehaviour
    6. {
    7.     // Variables
    8.     private GameObject npc; // The None playable character
    9.     private bool triggering; // check if the player is collider with NPC
    10.  
    11.     // Sounds
    12.     public AudioSource npcLine1;
    13.     public AudioSource playerAnswer1;
    14.  
    15.     public AudioSource npcLine2;
    16.     public AudioSource playerAnswer2;
    17.  
    18.  
    19.     private void OnTriggerEnter(Collider other)
    20.     {
    21.         if(other.tag.Equals("NPC"))
    22.         {
    23.             // If the NPC has tag "NPC", then set the game object to triggeringNPC
    24.             triggering = true;
    25.             npc = other.gameObject;
    26.  
    27.             // Play "Hello" + answer
    28.             npcLine1.Play();
    29.             playerAnswer1.Play();
    30.  
    31.             // Play "Mission" + answer
    32.             npcLine2.Play();
    33.             playerAnswer2.Play();
    34.         }
    35.         else
    36.         {
    37.             print("TAG=" + other.tag);
    38.         }
    39.     }
    40.     private void OnTriggerExit(Collider other)
    41.     {
    42.         // If the NPC has tag "NPC", then set the game object to null
    43.         triggering = false;
    44.         npc = null;
    45.     }
    46. }
    47.  
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    DoulasPoliceStation.cs is on Douglas@Idle, but you're checking other.tag. That means you're checking the tag of the object that triggers Douglas@Idle, not Douglas@Idle. You should probably be checking the tag for "Player" instead, and remember to set that tag for the player object in the inspector.
     
  3. Europa2019

    Europa2019

    Joined:
    May 23, 2019
    Posts:
    5
    Thanks, that worked.