The difference between these lines?

Can somebody tell me why this works:

        var ps = dustTrail.GetComponent < ParticleSystem>();
        var mains = ps.main;
        mains.startColor = Color.blue;

but this doesn’t?:

        var dustColor = dustTrail.GetComponent<ParticleSystem>().main.startColor;
        dustColor = Color.blue;

It’s not much of a problem but i do feels as if it’s something i should know.

Its simple:,
structs are value type, not reference type; this means that if you edit an struct the changes will be locally (stack memory), not in the heap.

A class is diferent (ParticleSystem is a class), is a reference type (heap memory). In your first example you are modifying the property color of the particleSystem object.
in your second example you are modifying a local struct of type color but the ParticleSystem object will not be notifyed because dustColor is not part of the ParticleSystem object.

I recommend that you read this :slight_smile:
https://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net

In the first one (the one that works) you’re changing startColor. In the second one (that doesn’t work) you’re changing startColor.color. I’m going to assume the latter variable either doesn’t exist or doesn’t do what you want.

[EDIT: Their original unedited post had them setting dustColor.color, but they edited it after my answer.]