Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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

Discussion in 'Scripting' started by adsbuenter, Dec 14, 2019.

  1. adsbuenter

    adsbuenter

    Joined:
    Nov 16, 2016
    Posts:
    2
    This is my script, i am trying to upgrade from unity 5.4 to Unity 2017.
    Code (CSharp):
    1.    private IEnumerator BasicFlameScale(float time)
    2.     {
    3.         float timer = 0f;
    4.         while (timer <= 1f)
    5.         {
    6. //error here  
    7.             FlameEffectVariables.ParticleBasicFlame.main.startSize = Mathf.Lerp(0.1f, 0.9f, timer);
    8.             timer += Time.deltaTime / time;
    9.             yield return null;
    10.         }
    11.     }
    12.  
    13. ///and this line of code
    14.  
    15.     public void SetBasicFlameScale(float scale)
    16.     {
    17.         if (m_basicFlameScaleRoutine != null)
    18.         {
    19.             StopCoroutine(m_basicFlameScaleRoutine);
    20.         }
    21. //error here        
    22. FlameEffectVariables.ParticleBasicFlame.main.startSize = scale;
    23.     }
    24.  
    25.  
    (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.
     
  2. adsbuenter

    adsbuenter

    Joined:
    Nov 16, 2016
    Posts:
    2
    And another one much more complicated when I tried to change the color overtime.
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. [ExecuteInEditMode]
    5. public class particleColorChanger : MonoBehaviour
    6. {
    7.     private void Update()
    8.     {
    9.         for (int i = 0; i < this.colorChangeList.Length; i++)
    10.         {
    11.             for (int j = 0; j < this.colorChangeList[i].colored_ParticleSystem.Length; j++)
    12.             {
    13. //Error from here
    14.               colorChangeList[i].colored_ParticleSystem[j].colorOverLifetime.color = this.colorChangeList[i].customer_Gradient;
    15.             }
    16.         }
    17.     }
    18.  
    19.     public particleColorChanger.colorChange[] colorChangeList;
    20.  
    21.     [Serializable]
    22.     public class colorChange
    23.     {
    24.         public string Name;
    25.  
    26.         public ParticleSystem[] colored_ParticleSystem;
    27.  
    28.         public Gradient customer_Gradient;
    29.     }
    30. }
    31.  
    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
     
  3. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    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 :
    Code (CSharp):
    1. private IEnumerator BasicFlameScale(float time)
    2. {
    3.     float timer = 0f;
    4.     ParticleSystem.MainModule _main = FlameEffectVariables.ParticleBasicFlame.main;
    5.     while (timer <= 1f)
    6.     {
    7.         _main.startSize = Mathf.Lerp(0.1f, 0.9f, timer);
    8.         timer += Time.deltaTime / time;
    9.         yield return null;
    10.     }
    11. }
    12.  
    13. public void SetBasicFlameScale(float scale)
    14. {
    15.     if (m_basicFlameScaleRoutine != null)
    16.     {
    17.         StopCoroutine(m_basicFlameScaleRoutine);
    18.     }
    19.  
    20.     ParticleSystem.MainModule _main = FlameEffectVariables.ParticleBasicFlame.main;
    21.     _main.startSize = scale;
    22. }
    and so :
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class particleColorChanger : MonoBehaviour
    3. {
    4.     private void Update()
    5.     {
    6.         for (int i = 0; i < this.colorChangeList.Length; i++)
    7.         {
    8.             for (int j = 0; j < this.colorChangeList[i].colored_ParticleSystem.Length; j++)
    9.             {
    10.                 ParticleSystem.ColorOverLifetimeModule _col = colorChangeList[i].colored_ParticleSystem[j].colorOverLifetime;
    11.                 _col.color = this.colorChangeList[i].customer_Gradient;
    12.             }
    13.         }
    14.     }
    15.  
    16.     public particleColorChanger.colorChange[] colorChangeList;
    17.  
    18.     [Serializable]
    19.     public class colorChange
    20.     {
    21.         public string Name;
    22.  
    23.         public ParticleSystem[] colored_ParticleSystem;
    24.  
    25.         public Gradient customer_Gradient;
    26.     }
    27. }
    Edit: fixed getting the mainModule in the BasicFlameScale method for better performance.
     
unityunity