- Home /
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.
Answer by Reynarz · Jun 18, 2018 at 09:14 PM
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 :) https://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net
Answer by SlowCircuit · Jun 18, 2018 at 08:49 PM
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.]
I actually looked right over that little difference so thank you for telling me, although i just removed the "color" variable and i still get the same result :/
That's cause now the second thing is just changing a color to a different color. dustColor is just not a reference, but a copy of the startColor you're setting it to. So when you change it, you're just changing the copy. The top way you're doing it is the more proper way.
How I'd do it:
var main = dustTrail.GetComponent < ParticleSystem>().main;
main.startColor = Color.blue;
Your answer
Follow this Question
Related Questions
Problem using point.Length in a particle system c# 2 Answers
How to convert new unity input system input actions to booleans 1 Answer
Particle distribution on mesh: How to implement a density map? 0 Answers
How do I set the position of a newly emitted particle in a local enabled particleSystem? 1 Answer
Unityscript - Using "as" to cast for GetComponentsInChildren returns empty array 1 Answer