- Home /
c# and game objects active state issues
hello, I've tried finding things online and either I'm not entering the right words into my query, or the hits don't quite fit my needs, and I cant see the link to making it such.
Basically, what I'm currently getting frustrated with is how to get game effects working on a proper linked structure, and not a per frame mash up. as in seamlessly running audio or bools that act as if you only had one effect going off as true.
I'm hoping to kill a few birds with this post, so if its not an easy answer ill break it into new questions with my script examples specifically.
1st is audio: I've tried AudioSource with several clips lined up. I've tried audio.clip = x and a for loop with an audio.clip array where I tried to base it on if audio.isplaying == false then audio.play. but no matter what I do, it always jumps threw the clips just like mashing the next track button on a stereo. the same is true for a sound effect attached to a getaxis input
2nd issue with audio and particle effects to be linked just as 1st issue. If I destroy the game object and have an explosion sound and particle effect at the time of boom, the audio and effect cut off when I destroy game object, but if I add a wait for seconds then the game object is still controllable and active till the effects are done and it then triggers the destroy game object.
So can you have the particle effect and audio do a single shot play which continues to finish its sequence after the destroy.
Thank you hope this was clear.
Answer by whydoidoit · Aug 22, 2013 at 07:00 PM
Ok so to your first point:
Playing a list of clips
Code like this perhaps:
[RequireComponent(typeof(AudioSource))]
public class PlayClips : MonoBehaviour
{
public AudioClip[] clips;
void Start()
{
StartCoroutine(Play(clips));
}
public IEnumerator Play(IEnumerable<AudioClip> clips)
{
foreach(var clip in clips)
{
audio.clip = clip;
audio.loop = false;
audio.Play();
yield return new WaitForSeconds(clip.length);
}
}
}
To your second question - you should establish the particle effect and audio source as a separate object instantiated or positioned where the first one is when it is destroyed.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to destroy a gameobject by GameObject.Find 1 Answer
Multiple Cars not working 1 Answer
Audio Trigger 3 Answers
Every few seconds, play an audio clip 3 Answers