- Home /
 
               Question by 
               RKM_91 · Apr 10, 2015 at 09:28 PM · 
                editoraudiosource  
              
 
              AudioSource not playing sound, Editor crashes?
Hi UnityAnswers,
I am trying to play a sound when the player walks into the cube? At the moment the game crashes when the player collides with the cube?
Any ideas, help, suggestions would be appreciated.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Trigger : MonoBehaviour 
 {
     public GameObject guiObject;
     private Question1 guiScript;
     public AudioSource collision;
 
     
     void Awake ()
     {
         guiScript = guiObject.GetComponent<Question1> ();
         guiScript.enabled = false; //Turn off the script, so the OnGUI function doesn't draw the question window
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (collision)
             audio.Play();
         guiScript.enabled = true;
         Destroy (this.gameObject);
     }
 }
 
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by DoTA_KAMIKADzE · Apr 10, 2015 at 09:35 PM
Change lines #20-21 to this:
 if (collision != null) collision.Play();
And my advise to you rename your AudioSource variable to something more obvious like cubeAudio or something, collision isn't the best name you could come up with really.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                