Adding Particles to a List?

Hey everyone!

Here is a quick code chunk, I’m trying to add particles to a list when they’ve been selected (this is just an example script tho, all the extra bits are removed). Usually I just tell something to add itself to a list if it’s not contained within the list, this allows it to only be added to the list once…blah blah blah…

But in this case…it seems to continuously add it anyway…so where have I gone wrong? In any case, if you can see what I’m trying to do and know of a better solution that would be appreciated as well!

Thanks in advance!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class asdfasdf : MonoBehaviour {
    ParticleSystem.Particle[] myPart;
    List<ParticleSystem.Particle> myPartList = new List<ParticleSystem.Particle>();
    List<string> myTestString = new List<string>();

    void Start () {
        myPart = new ParticleSystem.Particle[GetComponent<ParticleSystem>().maxParticles];
    }

    void Update () {
        //TODO : This works...
        /*
        if (!myTestString.Contains ("HI"))
            myTestString.Add ("HI");
            print (myTestString.Count);
        */

        //TODO : This Doesn't...
        GetComponent<ParticleSystem> ().GetParticles (myPart);

        for (int i = 0; i < GetComponent<ParticleSystem>().maxParticles; i++) {
            if(!myPartList.Contains(myPart[i])){
                myPartList.Add(myPart[i]);
                myPart[i].color = Color.red;
            }
            else{
                myPart[i].color = Color.green;
            }
        }

        GetComponent<ParticleSystem>().SetParticles(myPart, GetComponent<ParticleSystem>().maxParticles);
        print (myPartList.Count);
    }
}

Particles are structs not classes, so a comparison will compare the values for equality not the references.

So

if(!myPartList.Contains(myPart[i]))

Is not going to behave the way you expect. It will check for particles that have identical values.

Consider identifying your particles in a different way instead of reference, perhaps checking if they are red?

Ah, looks like I have a bit more to learn then…
Thanks for the response!

Unfortunately I was trying to add them to a list when they’ve been selected, the blue/red was just a visual check. Users can drag mouse to select particles and then click somewhere to have them move…at least this was the plan, now I’m not sure how to go about it.

Really appreciate the reply though, I’ll go back to the drawing board.

Providing you are not creating or destroying particles during selection you could store the index values for the selected ones.