- Home /
Radio Script not working
Hey guys, I have been stuck on this for a while but I still can't find solution. I want to make a radio script that I can turn on and off once I enter the trigger. It turns on but when I try to turn it off it just restarts the song. Any help is appreciated.
Here is my code:
#pragma strict
var Audio : AudioSource = GetComponent.<AudioSource>();
var CanPress : boolean = false;
var IsPlaying : boolean = false;
function Start ()
{
}
function Update ()
{
if(Input.GetKeyDown("e"))
{
if(CanPress == true)
{
if(IsPlaying == false)
{
Audio.Play();
}
}
if(CanPress == true)
{
if(IsPlaying == true)
{
Audio.Pause();
}
}
}
}
function OnTriggerEnter(other : Collider)
{
CanPress = true;
}
function OnTriggerExit(other : Collider)
{
CanPress = false;
}
Answer by Hellium · Jun 29, 2015 at 04:03 PM
Try the following. I know it's nearly the same, but some of your conditions are not exclusive. Maybe, it's the source of your problem, I don't know. Moreover, your code will be clearer this way. Also, you have forgotten to set the boolean IsPlaying. That's why the audio is rewinded and played instantly.
function Update ()
{
if(Input.GetKeyDown("e") && CanPress)
{
if(IsPlaying)
{
Audio.Pause();
IsPlaying = false;
}
else
{
Audio.Play();
IsPlaying = true;
}
}
}
Thanks for feed back !
Don't forget to accept the answer by clicking on the check mark under the vote buttons !
Your answer
Follow this Question
Related Questions
Assign a clip to an AudioSource when it finishes playing. 1 Answer
How to play "audiosource" component in a prefab from script? 0 Answers
How can I play multiple audioclips from the same object? 2 Answers
Play audio clip without a variable 1 Answer
Is there a way to create a random Audiosource loop? 2 Answers