Why this Particle System Script not working?

I am writing this script to increase car speed and Play multiple Particle System with a tag:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerJumpBoost : MonoBehaviour
{
    public float boostForce = 10;
    public float boostDuration = 1;
   

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Car" && other.GetComponent<CarController>())
        {
            StartCoroutine(other.GetComponent<CarController>().I_JumpBoost(boostForce, boostDuration));
            
            GameObject[] Particles;
            Particles = GameObject.FindGameObjectsWithTag("Boost");
            foreach(GameObject boost in Particles)
            {
                Particles.GetComponent<ParticleSystem>().Play();
            }
        }
    }
}

But this is not working and giving this error:
error CS1061: ‘GameObject[ ]’ does not contain a definition for ‘GetComponent’ and no accessible extension method ‘GetComponent’ accepting a first argument of type ‘GameObject[ ]’ could be found (are you missing a using directive or an assembly reference?)

Change: Particles.GetComponent<ParticleSystem>().Play();
To: boost.GetComponent<ParticleSystem>().Play();

“Particles” is the array, while “boost” is an object in the array.

1 Like

Beat me to it. :roll_eyes:

1 Like

Thank you so much man :). Never saw a forum with so many active and helpful users. you guys are awesome.
Now my code is this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerJumpBoost : MonoBehaviour
{
    public float boostForce = 10;
    public float boostDuration = 1;
  
  

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Car" && other.GetComponent<CarController>())
        {
            StartCoroutine(other.GetComponent<CarController>().I_JumpBoost(boostForce, boostDuration));

            GameObject[] Particles;
            Particles = GameObject.FindGameObjectsWithTag("Boost");
            foreach(GameObject boost in Particles)
            {
                boost.GetComponent<ParticleSystem>().Stop();
                var main = boost.GetComponent<ParticleSystem>().main;
                main.duration = boostDuration;
                boost.GetComponent<ParticleSystem>().Play();
            }
        }
    }
}

I wanted to add Particle duration also. But it’s giving me this error:
Don’t set duration while system is playing!
UnityEngine.MainModule:set_duration(Single)

“Play on awake” is disabled from Particle System Main Settings.
Please help me a little more.

Is the error on line 24?

1 Like

Yes.

That’s really weird … that should work according to the official documentation. Maybe it doesn’t like being inside the OnTriggerEnter function for some reason (unlikely to fix). Or maybe all of your particle systems need to be stopped before any of them can have their durations changed (maybe because they are all sharing the same particle system?). It’s also possible that you have some other code interfering, for example by doing Play() in the background.

I’m getting this error now too… and it’s crashed Unity for me.

An error like this wouldn’t crash Unity.
You might have an infinite loop somewhere, which would.

I have the same assertion with that code:

bool isPlaying = _system.isPlaying;
_system.Stop();

var main = _system.main;
main.duration = value;

if (isPlaying)
    _system.Play();

I am using Unity 2017.4.25f1 and ‘_system’ is a private readonly member.
the call is made in the main thread.
According to Unity’s documentation it should work, or have I missed something obvious?

What error are you seeing? On which line you are getting error ?

I have an assertion about the impossibility to change the duration of the particle system while it is running.
And that despite the fact that I stop it before setting the duration, as specified in the documentation.
It’s on line 5 in my example.

Well, this problem is 3 months old and I also forget how I solved this problem exactly .
What I remember is problem was not in Script but with collider in Car. I was using particle to create boost of car. So, first I was using just one trigger collider in car, Once trigger colliding, all code was running, and that’s why I was seeing error. But then I use 2 collider in car. One collider in front and second in back. First collider colliding first and helping only in getting boost Duration only. Second collider doing everything else for main script. Here is both script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerJumpBoost : MonoBehaviour
{
    public float boostForce = 10;
    public float boostDuration = 1;

    public static float BoostDur;
 

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Car" && other.GetComponent<CarController>())
        {
            BoostDur = boostDuration;
            StartCoroutine(other.GetComponent<CarController>().I_JumpBoost(boostForce, boostDuration));
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StopParticle : MonoBehaviour
{
    private ParticleSystem ps;
    public List<GameObject>                                     boostParticles = new List<GameObject> ();
    public List<GameObject>                                    smokeParticles = new List<GameObject> ();

    private CarAI carAI;

    private void Start()
    {
       
        }
    }

   void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Boost2")
        {
            foreach(GameObject boost in boostParticles)
            {
            ps = boost.GetComponent<ParticleSystem>();
            ps.Stop(); // Cannot set duration whilst Particle System is playing

            var main = ps.main;
            main.duration = TriggerJumpBoost.BoostDur;
            ps.Play();
            }

      
        }
    }
}

I hope it will help something.

One more thing. Are you changing duration of more than one Particle System/GameObject?