When I first got to know about the Observer pattern, I have always wanted to know if it was practical enough to put into actual use.
So recently I started to play around with the pattern and I will talk about the results in this post.
So here is my current understanding and interpretation of the Observer Pattern.

I am just going to delve into the application so for those that have never heard of the pattern, you can click here to find out more.


  1. Create a new Subject.
  2. Create a new Observer.
  3. Call Subject.attachObserver().
  4. Other scripts affect the Subject.
  5. Subject calls the setState() method which contains the notifyObservers method.
  6. Observer.update() iscalled The update method is where you put in all the code for handling subjects state change.



So we get something like this . . .




  1. Create a Health Subject that tracks players health.
  2. Created a MovementHealthObserver inherits Observer which observes the Health Subject.
  3. Called Health Subjects attachObserver function


So External Elements that calls the health Subjects setState() method would trigger all its Observers to know about the event happening and take action.

Over here I only have one Observer implemented but it is possible to add more. I will not be talking about the details of the implementation here,

do not wish to flood this post with code which I did in my previous post . . .

But if you wish for the code I have provided a link below this post.1


< Workaround for Unity3D Invoke() not accepting arguments >


I was trying to find a way to pause calling of a function for a length of time without using WaitForSeconds(), I found the Invoke() method, but it seems impossible to directly pass in my arguments to Unity`s Invoke() method.

So after doing a bit of scampering about for a solution, I got the following resolution.

So it`ll look something like this . . .

//Function declarations
IEnumerator storefunction;

    void invokethis()
    {
        StartCoroutine(storefunction);
    }

    IEnumerator IToggleDamage(int index, string name)
    {
        if(inmytrigger.Exists(x => x.gameobject.name == name))
        {
            inmytrigger[index] = new HandleValues(inmytrigger[index].gameobject, true);
        }
        yield return null;
    }


//Invoking the functions
//We store the Enumerator inside the storefunction variable first
float timetowait = 2.0f;
storefunction = IToggleDamage(index, name); //This is where we pass in our required arguments
//Then we cal Invoke on the invokethis method which contains a StartCoroutine method call with the storefunction variable as an argument
Invoke("invokethis",timetowait);


There will be another post where I will talk about using a Manager store player information

Edit: I realized that actually having the Observer component call the Subject.attachObserver method might have been a better idea . . .





(1) Download Code Implementation for Observer Patterns here