Sound not working

I have tried many examples. I’ve even tried scripting the sounds without any Empty Gameobjects and dynamically creating on. My last resort was to create 3 gameobjects with 1 of 3 sounds attached to them and calling the play method and I was still unable to get them to play. Any advice would be greatful.

void OnGUI()
	{
		Debug.Log(introPortion);
		if(introPortion == 0)
		{
			GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
			
			if(curRate == 9)
			{
				GameObject go = GameObject.Find("AudioObject1");
				AudioSource source = go.GetComponent<AudioSource>();
				
				StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText1.txt");
									
				string fileContent;
				
				fileContent = reader.ReadToEnd();
				
				reader.Close();
				
				GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
				//Debug.Log(GameObject.Find("Main Camera").GetComponent<AudioSource>().clip.length);
				StartCoroutine(Pause(source.clip.length,source));
			}
		}
		
		if(introPortion == 1)
		{
			GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
			
			if(curRate == 43)
			{
				GameObject go = GameObject.Find("AudioObject2");
				AudioSource source = go.GetComponent<AudioSource>();
				
				StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText2.txt");
				
				string fileContent;
				
				fileContent = reader.ReadToEnd();
				
				reader.Close();
				
				GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
				StartCoroutine(Pause(30.0f,source));
			}
		}
		
		if(introPortion == 2)
		{
			
			GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
			
			GUI.DrawTexture(new Rect(0,0,characterPorts[0].width/3,characterPorts[0].height/3),characterPorts[0]);
			GUI.DrawTexture(new Rect((Screen.width - (characterPorts[1].width/2)+120),0,characterPorts[1].width/3,characterPorts[1].height/3),characterPorts[1]);
			if(curRate == 77)
			{
				GameObject go = GameObject.Find("AudioObject3");
				AudioSource source = go.GetComponent<AudioSource>();
				source.rolloffMode = 0;
				source.volume = 100.0f;
				
				StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText3.txt");
				
				string fileContent;
				
				fileContent = reader.ReadToEnd();
				
				reader.Close();
				
				GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
				
				if(GUI.Button(new Rect(Screen.width - 130,Screen.height - 40,128,30),"Next"))
				{
					Application.LoadLevel("newGameScene");
				}
			}
		}
	}
	
	IEnumerator Pause(float waitTime, AudioSource go)
	{
		go.Play();
		Debug.Log("Paused");
		yield return new WaitForSeconds(waitTime);
		Debug.Log("Not Paused");
		if(introPortion < 2)
		{
			introPortion += 1;
			StopAllCoroutines();
		}
		else
			introPortion = 2;
	}

Lazerath

You may be playing them just fine, but the default settings for the audio will cause a decrease in volume over distance. If the instantiated object is too far away from the camera, you won’t hear them either way.

If the Debug.Log(“Paused”) is showing up, I’d make sure that the audio source is configured so that the mindistance and maxdistance are high numbers to avoid being unable to hear it. Also, you can configure the sound so that it isn’t a 3D sound, which would make it able to ignore distance from audio source.

Each of the sound files are 2d sounds, and I have the sound objects set to be the same position as the main camera in my scene. I get the Debug.Log(“Paused!”); but no sound. Everything else works just fine. Thanks for your suggestions m8, I tried them but still got nothing.

Lazerath

One last thing you can try… You know the routine is being run, so how about assigning the sounds by code as well?

var soundClip : AudioClip; // assign this in the inspector
go.clip = soundClip;
go.Play();

Or, to test and make sure your sounds can be heard normally, put them on an audio source and check “Play Automatically.”

Thanks m8, the assigning them in the inspector got them to play however they sound strange. Like they have a bit of reverb attached to them and are muddled. Any suggestions?

Thanks much m8 for the help.

Lazerath

Another problem. Using my gameobject with a audiosource attached to it I got the following error;

NullReferenceException
UnityEngine.GameObject.GetComponent (System.String type) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:185)
GUI_Introscene.OnGUI () (at Assets/Scripts/GUI_Introscene.cs:175)

The line it is referring to is:

source = obj.GetComponent("AudioSource") as AudioSource;

I tried:

source = obj.GetComponent<AudioSource>();

and still got the same error, it is as if it can’t find the object although I have the object placed in the hierarchy.

Lazerath

From all indications in Unity(which by the way I am using the free version if that has any barring on the problem I am having) the sound is playing yet I am getting nothing from my speakers. My hierarchy is like this:

Main camera with the script attached. In this script I have 3 variables exposed to the inspector[myClip1,myClip2,myClip3]. Within the inspector I set each of these variables to the three files I want to play in succesion. My source is the gameobject named “myAudioSource” which has the AudioSource component. This is the source that I use to set the clip and play of of. The problem is that when I call source.Play() and the debug this method to check if the source is actually playing the file I get a true, but I am getting no sound. If i call the PlayOneShot() on the same source I get a garbled sound from my speakers. Any ideas?

The Code:

void OnGUI()
	{
		//Debug.Log(introPortion);
		if(introPortion == 0)
		{
			GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
			
			if(curRate == 9)
			{				
				StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText1.txt");
									
				string fileContent;
				
				fileContent = reader.ReadToEnd();
				
				reader.Close();
				
				GameObject obj = GameObject.Find("myAudioSource");
				source = obj.GetComponent("AudioSource") as AudioSource;
				
				source.rolloffMode = 0;
				source.volume = 100.0f;
				
				source.clip = myClip1;
				//source.PlayOneShot(myClip1);
				
				source.Play();
				
				if(source.isPlaying)
					Debug.Log("Playing sound");
				
				GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
				//Debug.Log(GameObject.Find("Main Camera").GetComponent<AudioSource>().clip.length);
				//StartCoroutine(Pause(30.0f,myClip1));
			}
		}
		
		if(introPortion == 1)
		{
			GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
			
			if(curRate == 43)
			{				
				StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText2.txt");
				
				string fileContent;
				
				fileContent = reader.ReadToEnd();
				
				reader.Close();
				
				GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
				//StartCoroutine(Pause(35.0f,myClip2));
			}
		}
		
		if(introPortion == 2)
		{
			
			GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
			
			GUI.DrawTexture(new Rect(0,0,characterPorts[0].width/3,characterPorts[0].height/3),characterPorts[0]);
			GUI.DrawTexture(new Rect((Screen.width - (characterPorts[1].width/2)+120),0,characterPorts[1].width/3,characterPorts[1].height/3),characterPorts[1]);
			if(curRate == 77)
			{				
				StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText3.txt");
				
				string fileContent;
				
				fileContent = reader.ReadToEnd();
				
				reader.Close();
				
				//audio.PlayOneShot(myClip3);
				
				GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
				
				if(GUI.Button(new Rect(Screen.width - 130,Screen.height - 40,128,30),"Next"))
				{
					Application.LoadLevel("newGameScene");
				}
			}
		}
	}
	
	IEnumerator Pause(float waitTime, AudioClip go)
	{
		//audio.PlayOneShot(go);
		Debug.Log("Paused");
		yield return new WaitForSeconds(waitTime);
		Debug.Log("Not Paused");
		if(introPortion < 2)
		{
			introPortion += 1;
			StopAllCoroutines();
		}
		else
			introPortion = 2;
	}
}

Thanks for you help Sunman, I got it to work. It seemed as thou that having multiple clips and trying to load them at different intervals during my Intro script was causing some confusion to the engine. I just combined the clips into one clip, edited it for the correct amount of silence and made it play in the start function of my script. Played nicely, had to wiggle some of the wait times around to get it timed nicely but got it to work. Thanks for your time m8,

Lazerath

Actually, if a clip is playing you can’t switch the clip out. It doesn’t like it. In that case, you’d have to call source.Stop(); before switching the clip out and using source.Play();