- Home /
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 ?
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.
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 !
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;
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
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 .
"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 !
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
Thanks, beacuse I'm affraid I have to much information for my brain :D
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.
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).
Your answer
![](https://koobas.hobune.stream/wayback/20220613152909im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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