That’s correct. The only exception are static
members. For example, we are able to call static
methods on the class, and we cannot call static
methods on objects. A static
method is, for example, Mathf.Clamp(). We cannot create an object of type Mathf and call Clamp on it. Feel free to test that. The compiler will complain immediately. Read the error message.
Here is another example for a static method: AudioSource.PlayClipAtPoint(audioClip);
. AudioSource is not a variable but the classname. In your IDE, hover your mouse over PlayClipAtPoint to get a little popup window with additional information.
PlayOneShot is a non-static
method in the AudioSource class, hence it requires an object. C# does not care where the object comes from. GetComponent<AudioSource>()
returns the first AudioSource object it finds. If it does not find any, it returns null
, and you will get a NullReferenceException error.
If you find GetComponent(AudioSource).PlayOneShot(audioClip);
difficult to grasp, you could write it this way:
AudioSource as = GetComponent(AudioSource)
as.PlayOneShot(audioClip);
I named the variable as
, so my example does not confuse you. Of course, I could have named it audioSource
but for the sake of clarity, I opted for as
.
Without doing this would it be like trying to call a method on ‘float’ rather than an actual number?
What do you mean by that? Do you have an example?
Is that basically what caching is, and the point of caching?
The computer immediately “forgets” things. We create variables to keep results over time. The reason could be to improve the readability of our code, or because recalculating something is too expensive, or because we must “remember” the value for some other reason.
In C#, objects do not have a name. For this reason, we assign a value to a variable and do things with the variable. In the past, we had to remember the memory address to find our data again. That’s not necessary anymore. The computer does that for us, and we simply use variables.
There’s still one thing I don’t understand conceptually. Doesn’t AudioSource.PlayOneShot(audioClip)
already have an object: audioClip?
audioClip
is not an object. It is a variable. And that variable may or may not reference an object. We also do not pass on the variable but the value of the variable.
why does ‘audioSource = GetComponent();’ have to be declared inside another method?
It does not matter where you look for the reference. What matters is: audioSource
must not be null
when you call audioSource.PlayOneShot(clip);
. Since GetComponent is a relatively slow method, it might make sense to cache the reference to the AudioSource object instead of calling GetComponent multiple times to look for the same AudioSource object. “Multiple times” is the reason why caching might make sense.
If we call audioSource.PlayOneShot(clip);
only once during the lifetime of our object, GetComponent(AudioSource).PlayOneShot(audioClip);
is perfectly fine. The only reason for caching might be to increase the readability of the code. See my example with as
.
Apologies for asking so many questions, but thank you again for your kind help
As long as you are genuinely interested in the answers, please don’t apologise for asking questions.