PlayerPrefsController.SetMasterVolume();

Hello,

Could you please clarify how we were able to access SetMasterVolume() method from PlayerPrefsController script in the Test script?
Usually in order to call a method from another script we would need to use GetComponent<>() if it was part of the same object or FindObjectOfType<>().

Thank you

2 Likes

Hi Waleed,

Usually, we need a reference to access a method or a field inside an instance of a class. In this case, we would use GetComponent<T>() or another method to find the reference to the desired instance.

However, if, for example, a method is not meant to be part of a specific instance but meant to be more general, we can use the static keyword. It allows us to access the method directly via the class name and the dot notation.

When you read our PlayerPrefsController class, you’ll notice that we access the PlayerPrefs in the same way: PlayerPrefs.SetInt(...). This indicates that the static keyword is involved. It also means that you cannot use multiple PlayerPrefs instances because you cannot create any. The same applies to our PlayerPrefsController. And Mathf, which you used in other projects, e.g. Mathf.Clamp.

8 Likes

Great explanation, thank you very much Nina that was helpful.

You’re welcome. :slight_smile:


See also:

Privacy & Terms