Question by
piciorus · Jun 06, 2016 at 11:31 AM ·
audiosourceaudioplayscript reference
Audio play/stop c#
I had a open light script but i wish use him to open and stop music.
using UnityEngine; using System.Collections;
public class LightSwitch2 : MonoBehaviour {
public AudioClip sound;
public bool onSwitch;
public bool lightStatus;
public GameObject theLight;
void OnTriggerEnter(Collider other)
{
onSwitch = true;
}
void OnTriggerExit(Collider other)
{
onSwitch = false;
}
void Update()
{
if(theLight.active == true)
{
lightStatus = true;
}
else
{
lightStatus = false;
}
if (onSwitch)
{
if (lightStatus)
{
if (Input.GetKeyDown(KeyCode.E))
{
GetComponent<AudioSource>().PlayOneShot (sound);
theLight.active = false;
}
}
else
{
if (Input.GetKeyDown(KeyCode.E))
{
GetComponent<AudioSource>().PlayOneShot (sound);
theLight.active = true;
}
}
}
}
void OnGUI()
{
if (onSwitch)
{
if (lightStatus)
{
GUI.Box(new Rect(0, 0, 200, 20), "Press E to close the light");
}
else
{
GUI.Box(new Rect(0, 0, 200, 20), "Press E to open the light");
}
}
}
}
How do I change it,so I can start and stop the music?
Comment
Your answer
Follow this Question
Related Questions
play Audio Array in java 0 Answers
Having multiple audio sources in a single object? 0 Answers
Trying to use audio causes errors when trying to use in an 'if' statement 1 Answer
I can't get my sound to work! 1 Answer
Can not play a disabled audio source, but the source is definitely not disabled! 0 Answers