Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by juhainamtc · Nov 27, 2018 at 06:18 AM · buttonaudioaudiosourceaudioclip

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;

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges