- Home /
Dialogue System only plays the same words
Hi, 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()
{
int layer_mask = LayerMask.GetMask("Person");
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:
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
public Image faceChange;
public void TriggerDialogue()
{
faceChange.sprite = dialogue.Face;
Debug.Log("Talk");
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
Answer by Felipe_Schluepmann · Mar 09, 2021 at 01:14 PM
someone on stack exchange helped me figure this out
this is what he sent me:
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()
{
int layer_mask = LayerMask.GetMask("Person");
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613030419im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet bounce issue using Vector3.reflect 1 Answer
Using World Space, how would I bring up info on click. 0 Answers
ScreenPointToRay not responding 0 Answers
animation problem 1 Answer
How Do I See My RayCast? 1 Answer