Unity manual: "[RequireComponent (typeof(AudioSource))]" but none added

Hi there,
as everybody else, but it needs to get mentioned: “Great Course!”

In this lecture you use “PlayClipAtPoint(…)” method and when looking into unity manual it always states that a “Audio Source” component is needed to be attached to the object. But none is attached to the game object. This is bothering me. Why can we use “AudioSource.PlayClipAtPoint(…)” although we don’t have it as component?
Can we just use AudioSource because its in namespace unityEngine available?

(It’s just irritating that this requirement is stated everywhere in unity’s manual)

Hi,

Welcome to our community! :slight_smile:

First of all, let’s start with the definition from the API:

public static void PlayClipAtPoint(AudioClip clip, Vector3 position, float volume = 1.0F);

As you can see, there is the static keyword. Static things in C# do not have any object. static methods belong to a class. This means that the method is called directly on the class. We call static methods with the classname. In our case, the classname is AudioSource.

That’s why this line works without an AudioSource object/component:

AudioSource.PlayClipAtPoint(…);

The method creates a temporary game object with an AudioSource component in your Hierarchy to play the sound clip.

Did this clear it up for you?


See also:

Hi Nina,
your explanation get me on the right track, thank you :grinning:

At the end the explanation that enlighted me were yours and these two quotes which I will leave here if anybody else is thinking about it as well.

“A static method, field, property, or event is callable on a class even when no instance of the class has been created.” …and this example…:
“For example, your enemy class would have a static enemyCount; This variable will be the same for all enemies as there will be only one instance of it. Shall you destroy an enemy object or all of them, the variable is still there, if you create a new object then he will access that same variable.”
(Quote-source: What exactly is static? - Unity Answers)

So, I think I finally get it and thank you for the reply.
(Sry that my reply took so long, didn’t had the time the last days)

As a final prove of my understanding:

  • If I would execute AudioSource.PlayClipAtPoint() with a 3 second clip and during this time this command gets triggered somewhere else it would either crash or the first clip would end and the new clip starts.

Thanks and, if my prove does satisfy you that you teached me something :wink: - this can be closed :slight_smile:

1 Like

Fantastic. I’m glad I was able to help. :slight_smile:

By the way, where did you get the quotes from? Maybe you could share the source, so other students who have the same question as you could also learn from your answer (and do more research if they want).

Added the quote-source right in the post above.

Thank you :slight_smile:

1 Like

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

Privacy & Terms