Search code examples
c#unity-game-enginestructrefparticle-system

How to keep a reference to a Particle System module's struct in Unity


I'd like to keep a reference to a MinMaxCurve, a struct in some particle system module.

I need this ability to refer to any MinMaxCurve generically, rather than always dealing with the specific module and attribute.

Here's the gist:

public class ParticleAttributeMultiplier : ParticleAttributeAdjuster {

    new private ParticleSystem particleSystem;
    private ParticleSystem.EmissionModule emission;
    private ParticleSystem.MinMaxCurve minMaxCurve;

    public override void Start () {
        particleSystem = GetComponent<ParticleSystem> ();
        emission = particleSystem.emission;
        minMaxCurve = emission.rateOverTime;
    }

    public override void Input (float intensity) {
        minMaxCurve = 1000f;
    }
}

This does not work because the emission and rateOverTime are both structs, returned by value:

public struct EmissionModule
{
    // ... 
    public MinMaxCurve rateOverTime { get; set; } // also a struct
    // ... 
}

Is it possible, in any way, to store a reference to rateOverTime as a MinMaxCurve?


This seems to be the crux of the problem:

// "A property or indexer may not be passed as an out or ref parameter"
ref ParticleSystem.MinMaxCurve minMaxCurve = ref emission.rateOverTime;

Really appreciate any geniuses out there who can solve my problem.


Solution

  • You can't.

    ParticleSystem.MinMaxCurve is a structvalue type

    Just like e.g. Vector3 the variable is stored by value not using a reference like it would the case for a class.

    The only way is storing a reference to the according parent class which in your case is the particleSystem reference and access the value through it. You could get this reference stored by making it

     [SerializeField] private ParticleSystem _particleSystem;
    

    and drag it in via the Inspector.


    You can however store and adjust a generic ParticleSystem.MinMaxCurve value via the Inspector if that is what you are looking for

    [SerializeField] private ParticleSystem.MinMaxCurve minMaxCurve;