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
0
Question by Benjeta · Jul 01, 2014 at 07:34 AM · audiowaitforsecondsactivateclip

Script not working anymore

Hi, ^^

My script doesn't work anymore ! He desactivate all audio source when his audio source has finished to play (Line : 58) He don't reactivate the others audio source after !

Here my C# script :

 using UnityEngine;
 using System.Collections;
 using System;       //added to acces the enum class
 public class Sign : MonoBehaviour {
     private bool revertFogState = false;
     public Font f;
     public int SignsFound = 0;
     public AudioClip pickSound;
     public AudioClip DANCE;
     public Transform explosion;
     public Transform win;
     public Transform map;
     private GameObject toDestroy;
     private bool destroyObjectSwitch;
     private bool pickup;
     private bool collided;
     private int tempo;
     
     void Start () {
         this.destroyObjectSwitch = false;
         this.pickup = false;
         this.collided = false;
     }
     
     void Update () {
         if(Input.GetButton("Fire1")){
             this.pickup = true;
         }else{
             this.pickup = false;
         }
         Debug.Log ("pickup : "+this.pickup+" Collided : "+this.collided);
         if (Input.GetButtonDown("Pickup") && this.collided == true){
             AudioSource.PlayClipAtPoint(pickSound, transform.position );
             Instantiate(explosion, transform.position, transform.rotation);
             Destroy(this.toDestroy.gameObject);
             this.SignsFound++;
             this.pickup = false;
             this.collided = false;
             this.tempo = 100;
             if (SignsFound == 1){
                 StartCoroutine(PlaySound());
             }
             
             
 
         if(this.tempo>0)this.tempo--;
     }
     }
     
                 IEnumerator PlaySound(){
                 AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
                 foreach(AudioSource aud in audios)
                     aud.volume = 0;
                 AudioSource.PlayClipAtPoint(DANCE, transform.position );
                 Instantiate(win);
         RenderSettings.fog = revertFogState; 
         GameObject.Destroy(map.gameObject);
                 yield return new WaitForSeconds(audio.clip.length);
 
         foreach(AudioSource aud in audios)
             aud.volume = 1;
         
             
     }
     void OnTriggerEnter(Collider other){
         Debug.Log (other.gameObject.name);
         if(other.gameObject.tag == "Sign" ){
             this.collided = true;
             this.toDestroy = other.gameObject;
         }
     }
     
     void OnTriggerExit(Collider other){
         this.collided = false;
         this.toDestroy = null;
     }
     
     void OnGUI(){
         if(this.tempo>0)
             GUI.Label (new Rect (Screen.width/2-100,Screen.height/2-450,400,800), "Tu as "+this.SignsFound+" panneau (x) SUR 5.");
         GUI.skin.font = f;
 
     }
 }

What the fact ?

Comment
Add comment · Show 6
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 HarshadK · Jul 01, 2014 at 07:58 AM 0
Share

It's quite unclear to me as what exactly your problem is.

I don't see where you are deactivating or reactivating your audio sources.

$$anonymous$$aybe you want to elaborate more on your problem so that it becomes quite clear as to what was expected behavior and what output you are currently getting.

avatar image Benjeta · Jul 01, 2014 at 08:42 AM 0
Share

Hi, sorry, i'm french and I don't want many errors but, I'll try. In fact, I want to stop all audio sources in my scene : foreach(AudioSource aud in audios) aud.volume = 0; and play a music when you pick 1 Object :(if (SignsFound == 1){) THEN reactivate ALL Audio sources when the music stop : ( yield return new WaitForSeconds(audio.clip.length); foreach(AudioSource aud in audios) aud.volume = 1;)

But He doesn't work anymore :/ The audio sources are shuts, but he's no reactivating them !

avatar image HarshadK · Jul 01, 2014 at 09:36 AM 0
Share

By any chance are you destroying the game object to which all your audio sources are attached?

Try Debug.Log to print values of your audio source objects before and after your foreach loop below:

 foreach(AudioSource aud in audios)
     aud.volume = 1;
avatar image Benjeta · Jul 01, 2014 at 09:45 AM 0
Share

It's good on this way :/

avatar image HarshadK · Jul 01, 2014 at 09:48 AM 0
Share

Did you try what Nerevar said in his answer?

Now the fact it does not reactivate the audio sources is that audio.clip.length might not exist(From what I see audio.clip should be null), try doing Dance.length ins$$anonymous$$d

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nerevar · Jul 01, 2014 at 09:38 AM

Hello,

There could be a simpler (and safer) way to do that, just put the listener volume to zero or pause it and set the IgnoreListenerPause or IgnoreListenerVolume to true on the AudioSource playing your music, this way you will just hear this AudioSource. Then unpause listener or increase volume when it is done.

Now the fact it does not reactivate the audio sources is that audio.clip.length might not exist(From what I see audio.clip should be null), try doing Dance.length instead .

Comment
Add comment · Show 16 · 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 Benjeta · Jul 01, 2014 at 09:53 AM 0
Share

"just put the listener volume to zero or pause it" This is not what I have done in my script ? ^^

The problem is : I'm unable to reactivate other audio sources :| I have a problem after yield return new WaitForSeconds(DANC$$anonymous$$length); Nothing happened after !

avatar image Nerevar · Jul 01, 2014 at 09:59 AM 0
Share

Nope, you do that to each of your audioSources :) AudioListener.Pause is global, then you wouldn't need to reach all your audioSources and make one instruction for each of them, it is faster and safer.

I'll try your code and modify it :p

avatar image Benjeta · Jul 01, 2014 at 10:03 AM 0
Share

Thanks, beacuse I'm affraid I have to much information for my brain :D

avatar image Benjeta · Jul 02, 2014 at 03:04 PM 1
Share
 var walkSoundsWood : AudioClip[];
 var walkSoundsGrass : AudioClip[]; 
 var audioStepLength : int = 0.3;
 var groundType : int;
 var isPlayerWalking : boolean;
  
 function Update(){
   
 if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
 
 isPlayerWalking = true;
 }
 else{
  
 isPlayerWalking = false;
 }
 }
  
 function Start(){
 while(true){
 if (isPlayerWalking == true && groundType == 1){
 audio.clip = walkSoundsWood[Random.Range(0, walkSoundsWood.length)];
 audio.Play();
 yield WaitForSeconds(audioStepLength);
 }
 else if ( isPlayerWalking == true && groundType == 2){
 audio.clip = walkSoundsGrass[Random.Range(0, walkSoundsGrass.length)];
 audio.Play();
 yield WaitForSeconds(audioStepLength);
 }
 else{
 yield;
 }
 }
 }
  
  
 function OnTriggerStay(type : Collider){
 if (type.tag == "Wood"){
 groundType = 1;
 print("Wood");
 }
 else if (type.tag == "Grass"){
 groundType = 2;
 print("Grass");
 }
 }

Le voici ^^ j'aimerais ne pas les entendre justement.

avatar image Nerevar · Jul 02, 2014 at 03:13 PM 1
Share

Du coup essaye de remplacer ces lignes

 audio.clip = walkSoundsGrass[Random.Range(0, walkSoundsGrass.length)];
 audio.Play();

par

 AudioSource.PlayClipAtPoint(walkSoundsGrass[Random.Range(0, walkSoundsGrass.length)], transform.position);

pour chaque type de terrains que tu as.

L'autre solution serait de créer une variable AudioSource pour les pas et une autre pour DANCE et remplacer "audio" qui n'est pas safe par cette référence ( car là en fait il y avait plusieurs scripts qui tentaient d'accéder à la même source audio).

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

play audio for few seconds on key hit 1 Answer

help whit WaitForSecond use. 1 Answer

How to get the AI enemy character to play random audio clips over time? 1 Answer

Random, 3d sound. 1 Answer

Set audio clip to 3D in script 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