Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question NullReferenceException: Do not create your own module instances

Discussion in 'Scripting' started by little_box, Jul 2, 2021.

  1. little_box

    little_box

    Joined:
    Aug 22, 2017
    Posts:
    27
    NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance.
    Code (CSharp):
    1.  
    2. var efx = Instantiate(ParticleEffect, _controller.BoundsCenter, transform.rotation);
    3. efx.transform.SetParent(transform);
    4. efx.transform.localScale = Vector3.one;
    5. var vel = efx.velocityOverLifetime;
    6. vel.x = transform.lossyScale.x < 0 ? -0.1f : 0.1f;
    7. while (_dashing)
    8. {
    9.     yield return null;
    10. }
    11. yield return new WaitForSeconds(0.3f);
    12. var emission = efx.emission;
    13. emission.enabled = false;
    14. yield return new WaitForSeconds(5f);
    15. if(efx != null) Destroy(efx.gameObject);
    16.  
    This error occurs in
    Code (CSharp):
    1. emission.enabled = false;
    , happens in low frame rate.
    How to fix this error? Unity version is 2020.3.2.
     
    Last edited: Jul 3, 2021
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
  3. little_box

    little_box

    Joined:
    Aug 22, 2017
    Posts:
    27
    Hi, RadRedPanda, thanks for your reply. I'm trying to Change
    Code (CSharp):
    1. emission.enabled = false;
    to
    Code (CSharp):
    1. emission.rateOverTime = 0f;
    But this error still happen. This is probably due to some unknown Unity bug.