2

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.

  • https://stackoverflow.com/questions/2256048/store-a-reference-to-a-value-type possible duplicate question – TimChang Aug 14 '19 at 01:29
  • It's impossible, you can deal with the `EmissionModule` because it keeps a ref to the particle system, but `MinMaxCurve` doesn't. – shingo Aug 14 '19 at 03:52

1 Answers1

1

You can't.

ParticleSystem.MinMaxCurve is a struct &rightarrow; value 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;
derHugo
  • 83,094
  • 9
  • 75
  • 115