ParticleSystem collidesWith
I am trying to change collidesWith paramater of an particle system inside of a script but i am getting this error:
Error CS1612 Cannot modify the return value of 'ParticleSystem.collision' because it is not a variable
My Code:
GameObject ammo; //Game object with ParticleSystem on it
public LayerMask desiredLayers;
private void Start()
{
ammo.GetComponent<ParticleSystem>().collision.collidesWith = desiredLayers;
}
Now my question is what is the correct way to change the layers of a particle system collide with.
Answer by KronosAmon · May 25, 2020 at 08:08 PM
@richardkettlewell I asked this question because, on the official docs, there is no information about my question. But anyway I figure out what was the problem:
Unity has something special for ParticleSystem which uses Pointers so following code solved my problem:
var collision = ammo.GetComponent<ParticleSystem>().collision;
collision .collidesWith = desiredLayers;
The docs link contains a working example of how to modify collidesWith. So I’m not sure where it fails in answering your question: “what is the correct way to change the layers of a particle system collide with”
Your code, on the other hand, modifies a local variable, not a particle system property, and is incorrect.
@richardkettlewell In the docs it does exactly what i did, but i was confused because it seems like it was using a local variable, so i decided to ask.
In another forum someone explained that unity using pointers to deal with particle system and when you modify the "local variable" the changes are saved in the actual particle system property as well.
$$anonymous$$y code is working perfectly, I don't see why you think it is incorrect, if you know better way of doing it please share with us.
var collision = ammo.GetComponent<ParticleSystem>().collision;
collision.collidesWith = desiredLayers;
This is the correct code if you want to assign a new value to the collidesWith property in the particle system collision module (as per the docs, in the Update function in the example).
@richardkettlewell I just tested both of the codes (yours and $$anonymous$$e) and they both work perfectly.
As I see they do the exact same thing so I still don't understand why my code is "incorrect" while yours is not?".