- Home /
how to stop an audio source from playing multiple times; change button text
Hi there, I am trying to play a360 video and have imported its audio. Inside the video I have created a quiz Canvas, and a Score canvas. Inside the quiz canvas, at specific intervals mentioned in the script, the video should stop and the question shall be displayed. I have also added, two Prefab buttons onto the question canvas, which shall display the answer choices. Now, when the user clicks on an answer, the score is calculated and displayed onto the score canvas. The problem is that the Audio doesnt pause, and when I click on a choice button, the same audio is played simultaneously with the original audio , but from the point at which I clicked the answer button, for example, I click on the button at 55th second, the audio replays from the 55th second, in addition to the already playing audio. I want the audio and video to pause and play at the same time. Also, I have a script in which I have declared the variables for the Question array, as each question has a time(time at which to display the question), title and a bool value for correct answer.
Another help I need is that I want different choices to be displayed on these buttons for every question. Which means I need to access the button text and change it in the script, I used Getcomponent but it gives me "MissingComponentException". Here is my code for QuizManagerGameObject: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; //using UnityEngine.UI.Button;
public class QuizManager : MonoBehaviour {
//canvas where the question appears
public GameObject questionCanvas;
//question title
public Text questionTitle;
//score text
public Text scoreText;
//video player
public VideoPlayer player;
//audio
public AudioSource audsrc;
//flag to indicate whether we are showing questions or not
bool isShowingQuestions;
//Quiz
Quiz quiz;
//time(used to show questions)
float elapsedTime = 0;
//next question
Question nextQuestion;
//next question Index
int nextQuestionIndex;
//total correct answers
int totalCorrect = 0;
//total questions
int numQuestionsResponded =0;
void Start () {
//pause quiz
PauseQuiz();
//hide question canvas
questionCanvas.SetActive(false);
//prepare quiz first(questions of the quiz)
quiz = new Quiz();
quiz.questions = new Question[2];
quiz.questions[0] = new Question();
quiz.questions[0].time = 41;
quiz.questions[0].title = "The African Lion Population_____ dropped by over 50%";
quiz.questions[0].correct = true;
quiz.questions[1] = new Question();
quiz.questions[1].time = 140;
quiz.questions[1].title = "The lion _____ the camera";
quiz.questions[1].correct = true;
//prepare nrxt question
PrepareNext();
}
// Update is called once per frame
void Update () {
//check that we should be showing questions
if (!isShowingQuestions) return;
//increase the elapsed time by the amount that has happened since the last loop
elapsedTime += Time.deltaTime;
//check time, if a quesiton is due, show it
if(elapsedTime>nextQuestion.time)
{
//show question
//1)show question canvas
questionCanvas.SetActive(true);
//2) show the question title
questionTitle.text = nextQuestion.title;
//3) Pause the quiz
PauseQuiz();
}
}
void PauseQuiz()
{
//video paused
player.Pause();
//audio paused
audsrc.Pause();
//no showing question
isShowingQuestions = false;
}
void ResumeQuiz()
{
//continue playing video
player.Play();
//continue playing audio
audsrc.Play();
//continue measuring time elapsed
isShowingQuestions = true;
}
void PrepareNext()
{
//setting the first value
if(nextQuestion==null)
{
//set index to the start of the array
nextQuestionIndex = 0;
//get next question
nextQuestion = quiz.questions[nextQuestionIndex];
}
else
{
//increase the next question index
nextQuestionIndex++;
//check that there are more questions left
if(nextQuestionIndex < quiz.questions.Length)
{
//get next question
nextQuestion = quiz.questions[nextQuestionIndex];
}
else
{
//Quiz is over
print("Video completed!");
scoreText.text += "\nQuiz Completed!";
questionCanvas.SetActive(false);
player.Play();
return;
}
}
ResumeQuiz();
}
public void HandleAnswer(bool response)
{
print("responded:" + response + "Correct answer" + nextQuestion.correct);
//check if the answer was correct
//hide the question canvas
questionCanvas.SetActive(false);
//increase the number of responded questions
numQuestionsResponded++;
if(response ==nextQuestion.correct)
{
totalCorrect++;
scoreText.text = "Correct!";
}
else
{
scoreText.text = "Wrong";
}
scoreText.text += "\nScore:" + totalCorrect + "/" + numQuestionsResponded;
PrepareNext();
}
}
This is the code for Question.cs using System; using UnityEngine; using UnityEngine.UI;
public class Question {
//time in second
public float time;
//title of the question/text of the question
public string title;
//correct answer
public bool correct;
}
Code for Quiz.cs: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Quiz {
// array of questions
public Question[] questions;
}
Your answer
Follow this Question
Related Questions
Audio loops too early 2 Answers
No overlapping sounds 0 Answers
Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers
Can't seem to play my AudioClip? 2 Answers
Audio clip not playing 1 Answer