Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question ParticleSystem use Simulate and .SetCustomParticleData to set unique ID for each particle

Discussion in 'Getting Started' started by smoke_smk, Mar 1, 2024.

  1. smoke_smk

    smoke_smk

    Joined:
    Jun 30, 2017
    Posts:
    1
    Hello! Using the following code, I add an ID to each newborn particle and then use other code to collect data about the particles in each frame through particles ID.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections.Generic;
    4. [RequireComponent(typeof(ParticleSystem))]
    5. public class ExampleClass : MonoBehaviour {
    6.  
    7.     private ParticleSystem ps;
    8.     private List<Vector4> customData = new List<Vector4>();
    9.     private int uniqueID;
    10.  
    11.     void Start() {
    12.         ps = GetComponent<ParticleSystem>();
    13.     }
    14.  
    15.     void Update() {
    16.         ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
    17.         for (int i = 0; i < customData.Count; i++) {          
    18.             if (customData[i].x == 0.0f) // set custom data to the next ID, if it is in the default 0 state
    19.                 customData[i] = new Vector4(++uniqueID, 0, 0, 0);
    20.         }
    21.         ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
    22.     }
    23. }
    The problem is that when I run the particle system through the Play method, everything works like a charm! But I’ll try to do this using the Simulate(time, false, true, true) method. I believe that because the method resets the system, I lose all custom values on the system and in the new frame all particles do not have an ID value as newly created ones.

    Does anyone have any ideas on how to make the system work with the Simulation method and not reset the IDs of the particles? Thank you!