- Home /
Can an object check on other objects?
I want to create an "Audio Controller" game object that holds all of the sound clips for my game. However in order for that to work that object would need to be able to get information about all the other objects in the game that i want to associate sounds with.
For example, if my player ship comes into contact with an enemy bullet I want the ship and the bullet to call Destroy() while the Audio Controller calls explosion.Play().
By what method would I be able to pass information to the Audio Controller that this event has taken place?
Thanks for any advice!
Answer by Jwizard93 · Jul 10, 2017 at 09:43 PM
You can use an Event system that you can research in Unity's Documentation.
You could store a reference to every gameObject that needs to make a sound in the AudiController.
Or You could store a reference to the AudioController in every game object that makes a sound. I think I would do this option, but I'm not knowledgeable in Unity's audio classes.
So in the game obejct that needs to make a sound add
public AudioController myAudioController;
and when the sound is meant to be played call:
myAudioController.PlaySound(someVariableThatExpressesWhichSOundToPlay);
And drag the AudioController object onto the field for its reference in each object which will use it. That is done in the inspector for any object containing a script which declares a public reference for AudioController.
Wow thanks!
Just test this out in my game. Created an empty object with the AudioController script, loaded up some sound files, and was able to access them from another object!
Thanks dude!
Your answer
Follow this Question
Related Questions
Play audio at consistent volume 2 Answers
Looping an audio while holding "shift key" 1 Answer
Audiosource.Play() not working 0 Answers
AudioSource.clip.time won't work? 2 Answers
Assign a clip to an AudioSource when it finishes playing. 1 Answer