- Home /
ParticleSystem.emission randomly wont reset
Hi everybody, So, Im trying to gradually turn two particle systems on and off via code In Unity 5.3.1f1. The system are on a toggleable platform (which I've made into a prefab) and there are multiple duplicates of the same platform.
the problem is that, when i activate to turn off and on cycle a few times one or more random platform wont start emitting the particles again..
If i pause the and check the editor the emission module appears to be toggled off or on as it should, but the particles wont render.
here's the code I'm using:
using UnityEngine;
using System.Collections;
public class TimedPlatform : MonoBehaviour {
BoxCollider2D col;
float tempTime;
public Transform rayStart;
public Transform rayEnd;
ParticleSystem bricks;
ParticleSystem laser;
ParticleSystem[] particles;
ParticleSystem.EmissionModule em;
ParticleSystem.EmissionModule emTwo;
bool disabled = false;
Color bricksColor;
// Use this for initialization
void Start () {
particles = GetComponentsInChildren<ParticleSystem> ();
col = GetComponentInChildren<BoxCollider2D> ();
bricks = particles [0];
laser = particles [1];
bricksColor = bricks.startColor;
em = bricks.emission;
emTwo = laser.emission;
}
void DisablePlatform(){
emTwo.enabled = false;
em.enabled = false;
col.enabled = false;
}
IEnumerator TimeOut(){
yield return new WaitForSeconds (3);
DisablePlatform ();
}
void FixedUpdate(){
Vector3 startPoint = new Vector3 (rayStart.position.x, rayStart.position.y, 0);
Vector3 endPoint = new Vector3 (rayEnd.position.x, rayEnd.position.y, 0);
RaycastHit2D ray = Physics2D.Linecast(startPoint, endPoint);
Debug.DrawLine(startPoint, endPoint, Color.red);
if(ray.collider != null){
if(ray.collider.tag == "Player"){
if (!disabled) {
tempTime = Time.fixedTime;
bricks.startColor = Color.red;
disabled = true;
StartCoroutine (TimeOut ());
}
}
}
if (disabled) {
if (tempTime + 10f < Time.fixedTime) {
emTwo.enabled = true;
em.enabled = true;
bricks.startColor = bricksColor;
col.enabled = true;
disabled = false;
}
}
}
}
I've tried using a few variations of this, like using IEnumirators instead of fixedTime, and while the code seemed to work the end result was the same.
Any Ideas?
Answer by Jack_In_The_Sack · Jul 09, 2016 at 08:23 AM
If you haven't figured it out yet, I believe I know what the problem is.
Try turning all of the lines in your script that have startColor into comments. Then, test the game to see if the particles emit as they should. If they do, then startColor was causing the issue.
If the alpha of the color that you are trying to set startColor to is too low, then the particles will not render. You can manually set the alpha of the colors in the script by using color.a. Example: bricksColor.a = 255
Hopefully this helps.