- Home /
This question was
closed Feb 26, 2018 at 05:10 AM by
Ginxx009 for the following reason:
The question is answered, right answer was accepted
Question by
Ginxx009 · Dec 09, 2017 at 01:28 AM ·
c#arrayparticlesystem
Optimize the code (Array Unity)
Can someone help me to optimize this code .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThunderBehaviour : MonoBehaviour {
public AudioClip[] Thunder;
public AudioSource audsource;
private int _numberofparticles = 0;
private int count,count2;
public ParticleSystem partSys1,partSys2;
void Start(){
audsource = GetComponent<AudioSource> ();
}
void Update(){
StartCoroutine (ThunderSound());
}
IEnumerator ThunderSound(){
ParticleSystem.EmissionModule em1 = partSys1.emission;
ParticleSystem.EmissionModule em2 = partSys2.emission;
count = partSys1.particleCount;
count2 = partSys2.particleCount;
if(count > _numberofparticles && count2 > _numberofparticles){
audsource.clip = Thunder[Random.Range(0,Thunder.Length)];
audsource.Play ();
em1.enabled = false;
em2.enabled = false;
yield return new WaitForSeconds (audsource.clip.length);
em1.enabled = true;
em2.enabled = true;
}
}
}
I'm so pretty bad at optimizing code. Please someone help me. I'm trying to syncronize my thunder sound to my thunder particle system here but still i can't yet achieve it though i want first to optimize the code
Comment
If I read correctly, you activate your particle system once the sound is done playing ? Shouldn't the sound and the light start at the same time ?
I put a delay a lil bit like a nano second of it . hahaha for it to be realistic you know what i mean.
Best Answer
Answer by Ginxx009 · Dec 09, 2017 at 02:55 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThunderBehaviour : MonoBehaviour {
public AudioClip[] Thunder;
public AudioSource audsource;
private int _numberofparticles = 0;
private int count;
public ParticleSystem[] partSys1;
public GameObject[] partSys;
void Start(){
audsource = GetComponent<AudioSource> ();
}
void Update(){
StartCoroutine (ThunderSound());
}
IEnumerator ThunderSound(){
for(int p = 0; p < partSys1.Length; p++){
count = partSys1[p].particleCount;
if(count > _numberofparticles && count > _numberofparticles){
audsource.clip = Thunder[Random.Range(0,Thunder.Length)];
audsource.Play ();
for(int i = 0; i <partSys.Length; i++){
yield return new WaitForSeconds (partSys1[p].IsAlive);
partSys [i].SetActive (false);
yield return new WaitForSeconds (audsource.clip.length);
partSys [i].SetActive (true);
}
}
}
}
}
Optimized it by myself.