Passing 'this' vs a Monobehaviour class in the Action Scheduler

Just a little question on this lecture. In attempting the challenge, I used the key command:

 GetComponent<ActionScheduler>().StartAction(Fighter);

in the Fighter class (with similar code for Mover, natch).
This approach gave me the red squiggle of +1 sadness. And I couldn’t figure out why. Sam uses the solution:

 GetComponent<ActionScheduler>().StartAction(this);

I’d like to understand what is causing this. Fighter is a Monobehaviour, and so is the ‘this’ keyword, so I was surprised that my attempt didn’t work. Can someone 'splain?

Many thanks in advance!

2 Likes

Fighter is a class. A class is a definition of an object.
this is an instantiation of the class, it’s an actual object with the properties of a Fighter.
The ActionScheduler doesn’t need to know (or care) about the actual class, all it cares about is the instance that it is dealing with. When we pass this to ActionScheduler, we’re saying that we’re passing this particular instance of the Fighter class.

Fortunately, I wasn’t drinking a soda when I read that. Very clever, I may start using it.

1 Like

Ah yes that makes perfect sense and in retrospect I feel I bit silly asking! Its no different to passing any other argument.

Void DoStuff(int number)

Would not be called with DoStuff(int). Of course the parameter needs to be an instance.

I simply got confused because MonoBehavior is another layer of abstraction ‘up’ from Fighter.
Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms