error CS1612: Cannot modify a value type return value of `UnityEngine.ParticleSystem.main'.

This is my script, i am trying to upgrade from unity 5.4 to Unity 2017.

   private IEnumerator BasicFlameScale(float time)
    {
        float timer = 0f;
        while (timer <= 1f)
        {
//error here   
            FlameEffectVariables.ParticleBasicFlame.main.startSize = Mathf.Lerp(0.1f, 0.9f, timer);
            timer += Time.deltaTime / time;
            yield return null;
        }
    }

///and this line of code

    public void SetBasicFlameScale(float scale)
    {
        if (m_basicFlameScaleRoutine != null)
        {
            StopCoroutine(m_basicFlameScaleRoutine);
        }
//error here        
FlameEffectVariables.ParticleBasicFlame.main.startSize = scale;
    }

(242,58): error CS1612: Cannot modify a value type return value of `UnityEngine.ParticleSystem.main’. Consider storing the value in a temporary variable

I have tried to store in particle virable but it’s seem not working, anyone help me please.

And another one much more complicated when I tried to change the color overtime.

using System;
using UnityEngine;

[ExecuteInEditMode]
public class particleColorChanger : MonoBehaviour
{
    private void Update()
    {
        for (int i = 0; i < this.colorChangeList.Length; i++)
        {
            for (int j = 0; j < this.colorChangeList[i].colored_ParticleSystem.Length; j++)
            {
//Error from here
              colorChangeList[i].colored_ParticleSystem[j].colorOverLifetime.color = this.colorChangeList[i].customer_Gradient;
            }
        }
    }

    public particleColorChanger.colorChange[] colorChangeList;

    [Serializable]
    public class colorChange
    {
        public string Name;

        public ParticleSystem[] colored_ParticleSystem;

        public Gradient customer_Gradient;
    }
}

This is the error:
(13,78): error CS1612: Cannot modify a value type return value of `UnityEngine.ParticleSystem.colorOverLifetime’. Consider storing the value in a temporary variable
Anyone good at scripting, please help. Thank you

1 Like

The ParticleSystem.main and ParticleSystem.colorOverLifetime are not variables, they are interfaces and you need an instance before you can do anything with it, like so :

private IEnumerator BasicFlameScale(float time)
{
    float timer = 0f;
    ParticleSystem.MainModule _main = FlameEffectVariables.ParticleBasicFlame.main;
    while (timer <= 1f)
    {
        _main.startSize = Mathf.Lerp(0.1f, 0.9f, timer);
        timer += Time.deltaTime / time;
        yield return null;
    }
}

public void SetBasicFlameScale(float scale)
{
    if (m_basicFlameScaleRoutine != null)
    {
        StopCoroutine(m_basicFlameScaleRoutine);
    }

    ParticleSystem.MainModule _main = FlameEffectVariables.ParticleBasicFlame.main;
    _main.startSize = scale;
}

and so :

[ExecuteInEditMode]
public class particleColorChanger : MonoBehaviour
{
    private void Update()
    {
        for (int i = 0; i < this.colorChangeList.Length; i++)
        {
            for (int j = 0; j < this.colorChangeList[i].colored_ParticleSystem.Length; j++)
            {
                ParticleSystem.ColorOverLifetimeModule _col = colorChangeList[i].colored_ParticleSystem[j].colorOverLifetime;
                _col.color = this.colorChangeList[i].customer_Gradient;
            }
        }
    }

    public particleColorChanger.colorChange[] colorChangeList;

    [Serializable]
    public class colorChange
    {
        public string Name;

        public ParticleSystem[] colored_ParticleSystem;

        public Gradient customer_Gradient;
    }
}

Edit: fixed getting the mainModule in the BasicFlameScale method for better performance.