- Home /
How to break script execution when component adds to gameObject
I have scene witch has 5 cameras and 1 audio listener. 4 camera of 5 marked do not destroy on load. When I load another scene one of object with camera magically receives additional audio listener so after scene loaded I have 2 audio listeners and sound do not played!!!
I want to debug why audio listenr magically adds to my gameObject!
Some Body HELP
$$anonymous$$aybe can you give us some code ? The camera with audio listener is one of the four that are not destroy on load ? or it's the destroy one ? And the object which get the new audio listener is one of the 4 ?
Other thing, the camera which is magically received an audio listener is on a new object which is a prefab ? So you instantiate it i suppose ?
If it's a prefab just check you don't left the audio listener on the camera in your prefab.
If you create a camera and that you associate it to your object by default it gets an audioListener I think
it's not dostroy one.
Yes, object which get the new audio listener is one of the 4!
//Other thing, the camera which is magically received an audio //listener is on a new object which is a prefab ? So you //instantiate it i suppose ?
No! Camera created once and never destroyed anymore! And when New level loaded it magically receives audio Listener!
Answer by MibZ · Nov 02, 2012 at 05:11 PM
Not sure why additional components are being added, but this should do the trick.
Do you have code that adds the unexpected components in Start() or Awake() that you may have forgotten about?
EDIT: This code will only work if you put it into a script and attach it to each camera
void CheckListeners()
{
AudioListener[] temp = gameObject.GetComponents(AudioListener);
if (temp.Length > 1)
{
for (int i = 1; i <= temp.Length; i++)
Destroy(temp[i]);
}
return;
}
this works only if it's the gameObject with the Audiolistener which has the second one. :) I don't think it's the same btw. I'm waiting jes28 answer to say more.
No! I do not have code that adds Audio Listener at all!
I added bad code like this:
public class AudioListenerRemover : $$anonymous$$onoBehaviour
{
private void Update ( )
{
var listener = gameObject.GetComponent<AudioListener>( );
if( listener != null )
Destroy( listener );
}
}
And add it to all gameObjects that must no have AudioListener. Now it works but I want to find bug and remove this component in future!
Do you want the cameras to have no audio listener or only one? That code will make it so any camera that script is attached to will only be able to have an Audio Listener for one frame, if you want them to have only one you need to check for all Listeners attached to it.
I think it's better to find why the scene create a new one whereas try to patch this with script like this :/
Your answer
![](https://koobas.hobune.stream/wayback/20220613083235im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing speaker assignment dinamically in surround 5.1 1 Answer
Audio Listener and Overhead Camera 0 Answers
Audio listener to mono then to left or right speaker? 2 Answers
Mac built-in microphone stopped sending data to Unity. 0 Answers
In a split screen game, how to deliver different 3D sound? 5 Answers