[SOLVED] How to Store Object Player Entered Trigger of?
Hi there I've created a dialogue system that is meant to trigger when the player presses 'E' within the trigger of an NPC object. I was able to get it to work with only one NPC but after I made it a prefab and tried to make 2 it just grabs the dialogue from the first in the hierarchy which is pretty obvious looking at the code why it does that. I've been trying for at least an hour now to figure out how I'm supposed to store which NPC's trigger the player has entered but I can't seem to figure it out. Here's the code snippet of what I am trying to do:
if (inTrigger && Input.GetKeyDown(KeyCode.E))
{
FindObjectOfType<DialogueTrigger>().TriggerDialogue();
}
if (inConversation)
{
if (Input.GetKeyDown(KeyCode.Return))
{
FindObjectOfType<DialogueManager>().DisplayNextSentence();
}
}
}
void OnTriggerEnter(Collider other)
{
inTrigger = true;
currentNPC = other.collider.name;
}
So I can see why it finds the first one with the FindObjectOfType thing I have going what I also tried was doing:
DialogueTrigger questGiver = GetComponent<DialogueTrigger>();
questGiver.TriggerDialogue();
But then I always get an error stating I'm not referencing an instance of an Object. I've tried Googling it a bunch of different ways but I'm not really finding a solution. If anyone could help me out I'd really appreciate it :)
Answer by LinkUpGames · Jan 20, 2020 at 04:49 AM
Welp it seems really obvious now but I just changed
currentNPC = other.collider.name;
To
currentNPC = other.gameObject;
And that worked perfectly fine. Can't believe I missed that but you live and you learn!