Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can i add sound

Discussion in 'Scripting' started by Meliodass, Mar 16, 2015.

  1. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    I have this script and i want an alarm sound to trigger when the Hour turns to 12 noon Lunch time but it doesnt work well the sound doesnt play can you guys suggest what my mistake is? :(
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Clock : MonoBehaviour {
    6.  
    7.     public AudioClip[] bell;
    8.  
    9.     void OnGUI(){
    10.         DateTime time = DateTime.Now;
    11.         String hour =  String.Format("{0:hh}",time).PadLeft(2,'0');
    12.         String minute = time.Minute.ToString().PadLeft(2,'0');  
    13.         GUILayout.Label(hour+":"+minute);
    14.     }
    15.     void PlaySound(int clip){
    16.         GetComponent<AudioSource>().clip = bell[clip];
    17.         GetComponent<AudioSource>().Play ();
    18.     }
    19.     void Update()
    20.     {
    21.         DateTime time = DateTime.Now;
    22.         String hour =  String.Format("{0:hh}",time).PadLeft(2,'0');
    23.         String minute = time.Minute.ToString().PadLeft(2,'0');
    24.  
    25.         if (hour == "12")
    26.         {  
    27.             PlaySound (0);
    28.         }
    29.     }
    30. }
    31.  
     
  2. lE1NaD89

    lE1NaD89

    Joined:
    Feb 11, 2014
    Posts:
    6
    Not getting errors?
    Check if you have the volume high (System/Unity)

    using UnityEngine;
    using System.Collections;
    using System;

    public class Clock : MonoBehaviour {

    public AudioClip[] bell; //Check if you have any clip inside of the bell array;

    private AudioSource audioSource;

    void OnEnable(){
    audioSource = GetComponent<AudioSource>();

    if(audioSource == null){
    audioSource = gameObject.AddComponent<AudioSource>();
    }
    }

    void OnGUI(){
    DateTime time = DateTime.Now;
    String hour = String.Format("{0:hh}",time).PadLeft(2,'0');
    String minute = time.Minute.ToString().PadLeft(2,'0');
    GUILayout.Label(hour+":"+minute);

    if(hour == "12"){
    Debug.Log("Yap, the problem isn't here");
    PlaySound(0);
    }
    else {
    Debug.Log("Yap, the problem is here, problem with hour conversion/format");
    Debug.Log(hour);
    }
    }

    void PlaySound(int clip){
    audioSource.clip = bell[clip];
    audioSource.Play();

    if(audioSource.isPlaying){
    Debug.Log("The sound is working...");
    }
    }

    Not tested.
     
  3. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    From 12:00:00 up to 12:59:59, this code will stop the 'music' assign a new clip and start it. If the first miliseconds of the clip are silent/almost unhearable, you will not hear the audio being played; this is due to it constantly playing the first few miliseconds. Just make sure the clip is not already playing, only if that is the case start it... but that makes the audio loop of sorts.

    Make the code something like this:
    Code (CSharp):
    1. [SerializeField]//Makes private fields show up in the inspector.
    2. private AudioClip[] clips;
    3.  
    4. private void Update()
    5. {
    6.     DateTime d = DateTime.Now;
    7.     //It will restart the clip if it finishes within the first minute.
    8.     if(d.Hour==12 && d.Minute==0 /*&& d.Second==0*/)
    9.     {
    10.         PlaySound(clips[0]);
    11.     }
    12. }
    13.  
    14. public void PlaySound(AudioClip clip)
    15. {
    16.     AudioSource source = GetComponent<AudioSource>();
    17.     if (source.clip != clip)
    18.     {
    19.         source.clip = clip;
    20.     }
    21.     if (!source.isPlaying)
    22.     {
    23.         source.Play();
    24.     }
    25. }
    Should this not solve your issue, you could check whether a clip has been assigned. Otherwise the distance between the audio source and listener; if it is quite big, the fallof could make it unhearable. This seems very inconvenient to debug, but that should not be the issue...
     
  4. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    I tried your code thanks it says "The sound is working" but there's no sound playing but when I alt tab on Google chrome the sound played then i click on unity again the sound stops, I really don't know whats wrong with this :(
     
  5. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    @Mr. Mud i tried that sir but the audio didnt play. Sorry sir cos i just started with unity :(
     
  6. lE1NaD89

    lE1NaD89

    Joined:
    Feb 11, 2014
    Posts:
    6
    Mr.Mud is right about "From 12:00:00 up to 12:59:59, this code will stop the 'music' assign a new clip and start it."
    And i thing when you change the window, unity stops the Update Function, but the music not.

    correct:

    if(hour == "12" && !audioSource.isPlaying){
    PlaySound(0);
    }

    or

    if(hour == "12" && minute == "0" && second =="0" && miliSecond == "0"){
    PlaySound(0);
    }

    or use Mr.Mud code.

    And DateTime.Now; uses the current system time ?
    so if you are trying compare the hour with the current system hour, only works if they have the same hour. if your system hour is 14h and you are trying compare with 12, never works.
     
    Last edited: Mar 16, 2015
  7. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    @lE1NaD89 sir what do you mean by changing the clip and start it?
     
  8. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    That was actually a part he quoted from my reasoning of why it doesn't work. Once the time turns to 12:XX:XX, your script stops the currently playing clip, unassigns that clip and assigns the new clip (most of this happens at the background). After that command you start the newly assigned clip from the start. This will make it so that the clip starts over and over and over and... you get the point (I hope).

    Since this will happen multiple times per second, you will never get far enough in the clip to actually hear the clip (if it has a slight fade-in).

    About why my solution didn't work, I assume you did not reassign the intended clip. Any references to object are lost when you change the name of a variable (leaving "FormerlySerializedAttribute" out of the equation). Since you named it "bell", and I called it "clips", Unity will not keep them assigned by default.