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

Play and stop sound with two different buttons.

Discussion in 'Scripting' started by visualizy, Mar 19, 2016.

  1. visualizy

    visualizy

    Joined:
    Jul 11, 2013
    Posts:
    5
    I have two game objects 3D buttons named "red" and "green", I want to click on the "green" button to play a sound(in a loop) and click the "red" button to stop the same sound. How do I accomplish that?

    I tried the code below it works on the "green" button, but I'm not sure how to implement the code to turn music off using the "red" button. Any suggestions would be helpful.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SoundON : MonoBehaviour
    5. {
    6.     AudioSource audioSource;
    7.     public AudioClip mySound;
    8.  
    9.     void Start()
    10.     {
    11.         audioSource = GetComponent<AudioSource>();
    12.         audioSource.clip = mySound;
    13.     }
    14.  
    15.     void OnMouseDown()
    16.     {
    17.         audioSource.Play();
    18.      }
    19.  
    20. }
    21.  
     
  2. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    The same way but with Stop.
     
  3. visualizy

    visualizy

    Joined:
    Jul 11, 2013
    Posts:
    5
    I tried but it wasn't working. I attached a AudioSource to that game object it still gives me that error
    MissingComponentException: There is no 'AudioSource' attached to the "Stop" game object, but a script is trying to access it.
    You probably need to add a AudioSource to the game object "Stop". Or your script needs to check if the component is attached before using it.
     
  4. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    Hmm, I would think you need to access the same AudioSource, though. If it's playing a certain clip that you want to control, you would need the same AudioSource.
     
  5. visualizy

    visualizy

    Joined:
    Jul 11, 2013
    Posts:
    5
    Hm ok, so how I do i attach the same AudioSource?

    Maybe this method isn't that optimal.
    or is there any other efficient way to play/stop the sound with 2 different buttons?
     
  6. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    Make audiosource public and get the value of it in the other class and simply use .stop
     
  7. SeasiaInfotechind

    SeasiaInfotechind

    Joined:
    Nov 17, 2014
    Posts:
    32
    Hello victhevip,
    You need to attache same script on the both gameObject name "red" and "green" and AudioSource and make a check if gameobject is green than play or if gameobject is red stop it.

    void OnMouseDown()
    {
    if (gameObject.name == "green") {
    if (!audioSource.isPlaying)
    {
    audioSource.clip = mySound;
    audioSource.Play ();
    }
    }
    else if (gameObject.name == "red")
    {
    GameObject.Find ("green").GetComponent<AudioSource> ().Stop();
    }
    }

    Thanks