Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sound not working

Discussion in 'Scripting' started by Lazerath, May 15, 2011.

  1. Lazerath

    Lazerath

    Joined:
    Apr 28, 2011
    Posts:
    8
    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.

    Code (csharp):
    1.  
    2. void OnGUI()
    3.     {
    4.         Debug.Log(introPortion);
    5.         if(introPortion == 0)
    6.         {
    7.             GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
    8.            
    9.             if(curRate == 9)
    10.             {
    11.                 GameObject go = GameObject.Find("AudioObject1");
    12.                 AudioSource source = go.GetComponent<AudioSource>();
    13.                
    14.                 StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText1.txt");
    15.                                    
    16.                 string fileContent;
    17.                
    18.                 fileContent = reader.ReadToEnd();
    19.                
    20.                 reader.Close();
    21.                
    22.                 GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
    23.                 //Debug.Log(GameObject.Find("Main Camera").GetComponent<AudioSource>().clip.length);
    24.                 StartCoroutine(Pause(source.clip.length,source));
    25.             }
    26.         }
    27.        
    28.         if(introPortion == 1)
    29.         {
    30.             GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
    31.            
    32.             if(curRate == 43)
    33.             {
    34.                 GameObject go = GameObject.Find("AudioObject2");
    35.                 AudioSource source = go.GetComponent<AudioSource>();
    36.                
    37.                 StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText2.txt");
    38.                
    39.                 string fileContent;
    40.                
    41.                 fileContent = reader.ReadToEnd();
    42.                
    43.                 reader.Close();
    44.                
    45.                 GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
    46.                 StartCoroutine(Pause(30.0f,source));
    47.             }
    48.         }
    49.        
    50.         if(introPortion == 2)
    51.         {
    52.            
    53.             GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
    54.            
    55.             GUI.DrawTexture(new Rect(0,0,characterPorts[0].width/3,characterPorts[0].height/3),characterPorts[0]);
    56.             GUI.DrawTexture(new Rect((Screen.width - (characterPorts[1].width/2)+120),0,characterPorts[1].width/3,characterPorts[1].height/3),characterPorts[1]);
    57.             if(curRate == 77)
    58.             {
    59.                 GameObject go = GameObject.Find("AudioObject3");
    60.                 AudioSource source = go.GetComponent<AudioSource>();
    61.                 source.rolloffMode = 0;
    62.                 source.volume = 100.0f;
    63.                
    64.                 StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText3.txt");
    65.                
    66.                 string fileContent;
    67.                
    68.                 fileContent = reader.ReadToEnd();
    69.                
    70.                 reader.Close();
    71.                
    72.                 GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
    73.                
    74.                 if(GUI.Button(new Rect(Screen.width - 130,Screen.height - 40,128,30),"Next"))
    75.                 {
    76.                     Application.LoadLevel("newGameScene");
    77.                 }
    78.             }
    79.         }
    80.     }
    81.    
    82.     IEnumerator Pause(float waitTime, AudioSource go)
    83.     {
    84.         go.Play();
    85.         Debug.Log("Paused");
    86.         yield return new WaitForSeconds(waitTime);
    87.         Debug.Log("Not Paused");
    88.         if(introPortion < 2)
    89.         {
    90.             introPortion += 1;
    91.             StopAllCoroutines();
    92.         }
    93.         else
    94.             introPortion = 2;
    95.     }
    96.  
    Lazerath
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  3. Lazerath

    Lazerath

    Joined:
    Apr 28, 2011
    Posts:
    8
    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
     
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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."
     
  5. Lazerath

    Lazerath

    Joined:
    Apr 28, 2011
    Posts:
    8
    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
     
  6. Lazerath

    Lazerath

    Joined:
    Apr 28, 2011
    Posts:
    8
    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:

    Code (csharp):
    1. source = obj.GetComponent("AudioSource") as AudioSource;
    I tried:

    Code (csharp):
    1. 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
     
  7. Lazerath

    Lazerath

    Joined:
    Apr 28, 2011
    Posts:
    8
    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:

    Code (csharp):
    1.  
    2. void OnGUI()
    3.     {
    4.         //Debug.Log(introPortion);
    5.         if(introPortion == 0)
    6.         {
    7.             GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
    8.            
    9.             if(curRate == 9)
    10.             {              
    11.                 StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText1.txt");
    12.                                    
    13.                 string fileContent;
    14.                
    15.                 fileContent = reader.ReadToEnd();
    16.                
    17.                 reader.Close();
    18.                
    19.                 GameObject obj = GameObject.Find("myAudioSource");
    20.                 source = obj.GetComponent("AudioSource") as AudioSource;
    21.                
    22.                 source.rolloffMode = 0;
    23.                 source.volume = 100.0f;
    24.                
    25.                 source.clip = myClip1;
    26.                 //source.PlayOneShot(myClip1);
    27.                
    28.                 source.Play();
    29.                
    30.                 if(source.isPlaying)
    31.                     Debug.Log("Playing sound");
    32.                
    33.                 GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
    34.                 //Debug.Log(GameObject.Find("Main Camera").GetComponent<AudioSource>().clip.length);
    35.                 //StartCoroutine(Pause(30.0f,myClip1));
    36.             }
    37.         }
    38.        
    39.         if(introPortion == 1)
    40.         {
    41.             GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
    42.            
    43.             if(curRate == 43)
    44.             {              
    45.                 StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText2.txt");
    46.                
    47.                 string fileContent;
    48.                
    49.                 fileContent = reader.ReadToEnd();
    50.                
    51.                 reader.Close();
    52.                
    53.                 GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
    54.                 //StartCoroutine(Pause(35.0f,myClip2));
    55.             }
    56.         }
    57.        
    58.         if(introPortion == 2)
    59.         {
    60.            
    61.             GUI.DrawTexture(new Rect(0,0,1024,768),introAnimation1[curRate]);
    62.            
    63.             GUI.DrawTexture(new Rect(0,0,characterPorts[0].width/3,characterPorts[0].height/3),characterPorts[0]);
    64.             GUI.DrawTexture(new Rect((Screen.width - (characterPorts[1].width/2)+120),0,characterPorts[1].width/3,characterPorts[1].height/3),characterPorts[1]);
    65.             if(curRate == 77)
    66.             {              
    67.                 StreamReader reader = new StreamReader("Assets/Resources/Text Files/introText3.txt");
    68.                
    69.                 string fileContent;
    70.                
    71.                 fileContent = reader.ReadToEnd();
    72.                
    73.                 reader.Close();
    74.                
    75.                 //audio.PlayOneShot(myClip3);
    76.                
    77.                 GUI.Label(new Rect(7,Screen.height - 200,Screen.width - 14,175),fileContent,CustomStyle);
    78.                
    79.                 if(GUI.Button(new Rect(Screen.width - 130,Screen.height - 40,128,30),"Next"))
    80.                 {
    81.                     Application.LoadLevel("newGameScene");
    82.                 }
    83.             }
    84.         }
    85.     }
    86.    
    87.     IEnumerator Pause(float waitTime, AudioClip go)
    88.     {
    89.         //audio.PlayOneShot(go);
    90.         Debug.Log("Paused");
    91.         yield return new WaitForSeconds(waitTime);
    92.         Debug.Log("Not Paused");
    93.         if(introPortion < 2)
    94.         {
    95.             introPortion += 1;
    96.             StopAllCoroutines();
    97.         }
    98.         else
    99.             introPortion = 2;
    100.     }
    101. }
    102.  
    103.  
     
  8. Lazerath

    Lazerath

    Joined:
    Apr 28, 2011
    Posts:
    8
    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
     
  9. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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();