Dialogue System only plays the same words

I'm trying to modify brackeys dialogue system using raycast in an interactableOjects Script so that I can talk with multiple characters, however, when I talk to Character A it appears Characters B text and if I go to Character B the same text shows up (which in character B's Case wouldn't be a problem haha). is there a way that I can tell my script to identify both different objects and play different script (I mean without having to create different layers or tags)

InteractableObjects Script:

public void PersonInteract()
{

RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range, layer_mask))
{
Debug.Log("Speak");

FindObjectOfType<DialogueTrigger>().TriggerDialogue();

}
}


DialogueTrigger Script(the one that is attached to the NPC's):

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;

public void TriggerDialogue()
{
Debug.Log("Talk");
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);

}

}


FindObjectOfType searches your entire scene for the first GameObject with a component matching the type you asked for, even if that object has nothing whatsoever to do with the object you hit with the raycast.

If you want a DialogueTrigger on the object your raycast hit, then you should ask that object for its DialogueTrigger component:

public void PersonInteract()
{

RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range, layer_mask))
{
if (hit.transform.TryGetComponent(out DialogueTrigger trigger)) {
trigger.TriggerDialogue();
}
}
}


If your DialogueTrigger component sits at a different level of the hierarchy from the object that has the collider your raycast hits (ie. a child or parent object), then you might want GetComponentInChildren or GetComponentInParent instead.

• Thank you very much! this did the trick Mar 9, 2021 at 12:50