We use a private static method, Why?

Hello,

Our GetMouseRay() method in this lecture has the static keyword, why?

I have a feeling that this is an error, if so can anyone explain why you might ever use a private static method? I figure the whole point of making a method static is to access it from another script more easily and therefor would need to be private.

So my questions are:

  1. why did we use static here?
  2. if this was an error, why would we ever make a private static method?

Not all statics must be public.

Statics are useful when the actual state of the class that the function resides in is irrelevant.
A great example of statics can be found in the Mathf library. These methods are all static, as they do not in any way shape or form depend on any concrete implementation of a Mathf class.
The same can be said with our GetMouseRay(). It is not accessing any information in the PlayerController. In fact, the only information it is retrieving is from two other static functions, Camera.main.ScreenPointToRay() and Input.mousePosition.

Since there is no reason we would need this method outside of the PlayerController class, there is no need to expose it to the outside world, so a private static wil suffice.

2 Likes

Privacy & Terms