Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by kbabilinski · Jul 13, 2012 at 10:01 PM · c#audiosound

Audio Play Once

So i have a quest system and i need the audio to only play once, how would i do that ?

using UnityEngine; using System.Collections;

 public class QuestSystem : MonoBehaviour {
  
  // Quest things
  public bool isitCompleate;
  public bool Objectives_triger;
  public bool inturuption;
  
  
  
  // what quest?
  public bool Quest1;
  //Sounds
  public AudioSource audio_source;
     public AudioClip sound;
  public bool Mute;
  
  //Quest Audio
  public AudioClip audioname;
  public AudioClip QuestOneStart;
  public AudioClip QuestOneinturuption;
  public AudioClip QuestOneEnd;
 
  // Use this for initialization
  void Start () {
  Mute = false;
  
  GetNewAudio();
  sound = (AudioClip)Resources.Load("audioname");
  Quest1 = true;
  
  }
  
  // Update is called once per frame
  void Update () {
  audio_source.clip = sound;
  if (Quest1 == true)
  QuestOne();
  
  }
  
  void QuestOne(){
  
  // Confiremed start Audio / Objective
  if (isitCompleate == false){
  sound = QuestOneStart;
  if (Mute == false){
  if (!audio.isPlaying)
  audio_source.Play();
 
  
  }
  }
  
  
  
  if (inturuption == true){
  sound = QuestOneinturuption;
  if (Mute == false){
  if (!audio.isPlaying)
  audio_source.Play();
  }
  
  
  }
  
  if (isitCompleate == true){
  sound = QuestOneEnd;
  if (Mute == false){
  if (!audio.isPlaying)
  audio_source.Play();
  }
  
  }
  
  }
  void GetNewAudio(){
  
  audio_source = (AudioSource)gameObject.AddComponent("AudioSource");
    //     sound = (AudioClip)Resources.Load("audioname");
          
  }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Jul 14, 2012 at 04:13 PM

This seems to be a problem of logic: you're playing the sounds at Update, which occurs every frame - the first sound, for instance, will be repeated continuously while isitComplete is false. I suppose you're trying to make a state machine: play the first sound, then wait the question be answered or interrupted, then play the appropriate sound and pass to the next question, and so on - am I correct? If so, you could use coroutines: use a switch to select the question, then enter the question coroutine: a coroutine can play the start sound and wait for interruption or isitComplete to become true, then play the appropriate question and increment questNum, a variable that shows which question is selected. You should also add an AudioSource component to your object only once in the Editor, and use it for all sounds - adding an AudioSource for each sound is unnecessary, and wastes memory and time.

EDITED: Yielding to another coroutine in C# apparently doesn't chain it like in JS, thus I changed the code to just start the question coroutine and wait till it finishes.

using UnityEngine; using System.Collections;

public class QuestSystem : MonoBehaviour {

// Quest things public int questNum; // which question public bool isitComplete; public bool Objectives_triger; public bool interruption;

//Sounds public bool Mute;

//Quest Audio - clips for start, interrupt and end public AudioClip sndStart; public AudioClip sndInt; public AudioClip sndEnd;

void GetSounds(string sStart; string sInt; string sEnd){ // load the specified audio clips: sndStart = (AudioClip) Resources.Load(sStart); sndInt = (AudioClip) Resources.Load(sInt); sndEnd = (AudioClip) Resources.Load(s End); }

IEnumerator Start () { Mute = false; questNum = 1; // start with question 1 while (true){ switch (questNum){ case 1: StartCoroutine(Question1()); // start Question1 coroutine while (waiting) yield return 0; // wait until it finishes break, case 2: StartCoroutine(Question2()); // start Question2 coroutine while (waiting) yield return 0; // wait until it finishes break;

     // add here other questions as
     // new cases like above

     default: // invalid question number finishes script
       return;
     } 
   }
 }

}

bool waiting = false; // this flag shows that a question coroutine is running

// repeat this code for each question: IEnumerator Question1(){ waiting = true; // question coroutine started // get the sounds for this question: GetSounds("QuestOneStart", "QuestOneInterrupt", "QuestOneEnd"); // play the start sound: audio.PlayOneShot(sndStart); // do nothing while interruption and isitComplete are false: while (!interruption and !isitComplete){ yield return null; // let Unity free till next frame } if (interruption){ // if question interrupted play sndInt audio.PlayOneShot(sndInt); // if you want to abort the whole thing... questNum = -1; // set questNum to a non-existent question number } if (isitComplete){ // if question completed... // play sndEnd... audio.PlayOneShot(sndEnd); // and pass to the next question, if any: questNum++; } waiting = false; // question finished }

}

Comment
Add comment · Show 8 · Share
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
avatar image kbabilinski · Jul 14, 2012 at 04:46 PM 0
Share

Thank you :) but how do i make the audio play i fixed the little errors in your script like the inability to do yield return new. and now the script is error free but no sound plays

avatar image kbabilinski · Jul 15, 2012 at 12:10 AM 0
Share

;( still nothing

avatar image aldonaletto · Jul 15, 2012 at 07:28 PM 0
Share

I've just realized that *yield*ing to another coroutine in C# apparently doesn't work as expected: in JS, yielding to another coroutine works like a call to a regular routine - the caller code is resumed only when the routine finishes. I tried this in C#, but the coroutine yielded simply does nothing!


I edited my answer to include a brute force approach while this coroutine chaining mystery is unsolved.

$$anonymous$$ystery solved: in C#, you can't just yield to another coroutine this way:

 yield return OtherCoroutine();

this does simply nothing! You must ins$$anonymous$$d use StartCoroutine explicitly:

 yield return StartCoroutine(OtherCoroutine());

This isn't correctly informed in the docs (http://docs.unity3d.com/Documentation/ScriptReference/Coroutine.html)

avatar image kbabilinski · Jul 16, 2012 at 12:43 AM 0
Share

the lines
default: // invalid question number finishes script return;

dont work :(

avatar image aldonaletto · Jul 16, 2012 at 03:58 AM 0
Share

That's one more C# trap - it should be:

   default:
     return false;
 } 

Actually, this only finishes the Start coroutine (which was running in the background), and nothing else happens. You can use the default case to do other more useful things: I would set a boolean variable, and check this variable in Update, doing the appropriate stuff when it becomes set - like this:

bool endQuestions = false;

IEnumerator Start(){ ... default: endQuestions = true; return false; } }

void Update(){ if (endQuestions){ Application.LoadLevel(2); } }

Show more comments
avatar image
0

Answer by Mold · Jul 14, 2012 at 01:20 PM

Use the function audio.PlayOneShot(AudioFile);

Comment
Add comment · Share
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

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

6 People are following this question.

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

Related Questions

How To Turn Off Sound For Certain Objects? 1 Answer

Audio Scripting 1 Answer

How can I play audio while a non player controlled object is moving and stops playing when object stands still? 1 Answer

Audio listener to mono then to left or right speaker? 2 Answers

Why my sound it's not attaching to my Flashlight 2 Answers


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