RequireScript?

Hi,

I know there is a way to Require a specific compenent for a gameobject that has a specific script with the RequireComponent Attribute.

Now is there a way to do the inverse of this? To reqire a script to be attached to a gameobject which has a specific component.

For example for each game object that has an Audio Source I want to require a Audible script. (Just to make my example complete, Audible scripts Update function changes the pitch of the Audio Source based on Time.TimeScale)

Is there a way to enforce this in compile time? If not then how would you go about checking this at Runtime?

You could only do this if you had access to the AudioSource class.

At runtime, you could run this check at start:

AudioSource[] allAudio = FindObjectsOfType<Audiosource>();
foreach (AudioSource au in allAudio) {
if (!au.GetComponent<Audible>()) au.gameObject.AddComponent<Audible>();
}

But that wouldn’t catch any new objects as they get instantiated.

My recommendation would be to simply never use any AudioSources yourself at all - make the Audible script a complete manager class, it creates and destroys the AudioSource at your whim, and all sound functionality goes through your class instead of the AudioSource.

…or, upgrade to Unity 5, which (I’m guessing) has enough audio control to be able to handle whatever your Audible class is intended to handle.