Unity 3D: Observer Pattern with two game objects

Assuming we have one first person character and several NPC AIs.
The first person character should be able to talk to the NPC AI’s (via dialog service).
My problem is: How should I “connect” the two game objects (FPC and NPC) to each other?

So he can basically walk around and talk to them, then they answer something, and the first person character may choose an option.

I see several options here:

  1. add a public variable to ‘first person character’. this public variable will hold the NPC AI. So the NPC AI and first person character can talk to each other via functions on both classes.
  2. Add a class ‘DialogService’ which implements the observer pattern. So there will be several publishers and subscribers. My problem here is how to hook up the NPCs and first person character to each other
  3. Maybe you have a better alternative?

I am personally a fan of managers and singletons :slight_smile: Could you simply find all the AIs and add them to a List at the start of the level? You can manually (or programatically) add the character GameObject to a simple variable too. Maybe I’m missing something…

Or you want more details on how to find them. Check Unity - Scripting API: Object.FindObjectOfType

It is slow, but is OK to do at level load.