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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question How to change particle system radius during runtime

Discussion in 'Scripting' started by Marvin-Krist, Feb 24, 2022.

  1. Marvin-Krist

    Marvin-Krist

    Joined:
    Sep 25, 2015
    Posts:
    14
    Hello,

    I need to change the radius of a particle system over time, what I have right now is this :

    Code (CSharp):
    1.         this.gameObject.transform.Find("Steal Parent").gameObject.transform.Find("Steal Zone").gameObject.GetComponent<CircleCollider2D>().radius = xrad;
    2.         stealParticles = this.gameObject.transform.Find("Steal Parent").gameObject.transform.Find("Steal FX").gameObject.GetComponent<ParticleSystem>();
    3.         var stealShape = stealParticles.shape;
    4.         stealShape.radius = xrad;
    upload_2022-2-24_16-31-4.png

    With xrad value changing over time.

    The problem is, this code works only in the Start function, when I try to put it in Update function it does not work at all.

    How do I do that ?
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    There's no need for any of this insane code. All you do is this:
    Code (CSharp):
    1. [SerializeField]
    2. CircleCollider2D circle;
    3. [SerializeField]
    4. ParticleSystem stealParticles;
    5.  
    6. void YourCode() {
    7.   circle.radius = xrad;
    8.   var stealShape = stealParticles.shape;
    9.   stealShape.radius = xrad;
    10. }
    And just drag and drop the appropriate collider and particle system objects into the fields in the inspector. What you have right now is extremely fragile not to mention slow. There's also a ton of unecessary redundancy. If you insist on not using the inspector you can also just rewrite this in a much simpler way:
    Code (CSharp):
    1. transform.Find("Steal Parent/Steal Zone").GetComponent<CircleCollider2D>().radius = xrad;
    2. var stealParticles = transform.Find("Steal Parent/Steal FX").GetComponent<ParticleSystem>();
    3. var stealShape = stealParticles.shape;
    4. stealShape.radius = xrad;
     
    Kurt-Dekker and Marvin-Krist like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    As for why it doesn't work in Update - perhaps your object is disabled or destroyed, or perhaps the child objects that you're finding are moved in the hierarchy? Hard to say without more context.
     
    Marvin-Krist likes this.
  4. Marvin-Krist

    Marvin-Krist

    Joined:
    Sep 25, 2015
    Posts:
    14
    Thanks for your code advices, I'm making some more tests and I'll try to give you more informations
     
  5. Marvin-Krist

    Marvin-Krist

    Joined:
    Sep 25, 2015
    Posts:
    14
    Well, I did some tests, and I saw that the value of the particle system radius is properly increased over time but it does not impact how the particles are shown at all.

    I mean, when i change the value of the particle system radius directly into the inspector, it changes the particles behavior, but my code which also changes the value of the particle system radius has no impact on the particles behavior.

    Nothing is disabled, destroyed, moved in the hierarchy.

    Here is a screenshot of my object hierarchy :

    upload_2022-2-28_14-51-44.png

    Hope it helps.