Search Unity

Question Why this Particle System Script not working?

Discussion in 'Scripting' started by Only4gamers, Jul 26, 2020.

  1. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    I am writing this script to increase car speed and Play multiple Particle System with a tag:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TriggerJumpBoost : MonoBehaviour
    6. {
    7.     public float boostForce = 10;
    8.     public float boostDuration = 1;
    9.    
    10.  
    11.     void OnTriggerEnter(Collider other)
    12.     {
    13.         if (other.tag == "Car" && other.GetComponent<CarController>())
    14.         {
    15.             StartCoroutine(other.GetComponent<CarController>().I_JumpBoost(boostForce, boostDuration));
    16.            
    17.             GameObject[] Particles;
    18.             Particles = GameObject.FindGameObjectsWithTag("Boost");
    19.             foreach(GameObject boost in Particles)
    20.             {
    21.                 Particles.GetComponent<ParticleSystem>().Play();
    22.             }
    23.         }
    24.     }
    25. }
    26.  
    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?)
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Change:
    Particles.GetComponent<ParticleSystem>().Play();

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


    "Particles" is the array, while "boost" is an object in the array.
     
    Only4gamers likes this.
  3. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Beat me to it. :rolleyes:
     
    Only4gamers likes this.
  4. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TriggerJumpBoost : MonoBehaviour
    6. {
    7.     public float boostForce = 10;
    8.     public float boostDuration = 1;
    9.  
    10.  
    11.  
    12.     void OnTriggerEnter(Collider other)
    13.     {
    14.         if (other.tag == "Car" && other.GetComponent<CarController>())
    15.         {
    16.             StartCoroutine(other.GetComponent<CarController>().I_JumpBoost(boostForce, boostDuration));
    17.  
    18.             GameObject[] Particles;
    19.             Particles = GameObject.FindGameObjectsWithTag("Boost");
    20.             foreach(GameObject boost in Particles)
    21.             {
    22.                 boost.GetComponent<ParticleSystem>().Stop();
    23.                 var main = boost.GetComponent<ParticleSystem>().main;
    24.                 main.duration = boostDuration;
    25.                 boost.GetComponent<ParticleSystem>().Play();
    26.             }
    27.         }
    28.     }
    29. }
    30.  
    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.
     
  5. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Is the error on line 24?
     
    Only4gamers likes this.
  6. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Yes.
     
  7. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    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.
     
  8. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    I'm getting this error now too.. and it's crashed Unity for me.
     
  9. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    An error like this wouldn't crash Unity.
    You might have an infinite loop somewhere, which would.
     
  10. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    I have the same assertion with that code:
    Code (CSharp):
    1. bool isPlaying = _system.isPlaying;
    2. _system.Stop();
    3.  
    4. var main = _system.main;
    5. main.duration = value;
    6.  
    7. if (isPlaying)
    8.     _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?
     
  11. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    What error are you seeing? On which line you are getting error ?
     
  12. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    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.
     
  13. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TriggerJumpBoost : MonoBehaviour
    6. {
    7.     public float boostForce = 10;
    8.     public float boostDuration = 1;
    9.  
    10.     public static float BoostDur;
    11.  
    12.  
    13.     void OnTriggerEnter(Collider other)
    14.     {
    15.         if (other.tag == "Car" && other.GetComponent<CarController>())
    16.         {
    17.             BoostDur = boostDuration;
    18.             StartCoroutine(other.GetComponent<CarController>().I_JumpBoost(boostForce, boostDuration));
    19.         }
    20.     }
    21. }
    22.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StopParticle : MonoBehaviour
    6. {
    7.     private ParticleSystem ps;
    8.     public List<GameObject>                                     boostParticles = new List<GameObject> ();
    9.     public List<GameObject>                                    smokeParticles = new List<GameObject> ();
    10.  
    11.     private CarAI carAI;
    12.  
    13.     private void Start()
    14.     {
    15.        
    16.         }
    17.     }
    18.  
    19.    void OnTriggerEnter(Collider other)
    20.     {
    21.         if (other.tag == "Boost2")
    22.         {
    23.             foreach(GameObject boost in boostParticles)
    24.             {
    25.             ps = boost.GetComponent<ParticleSystem>();
    26.             ps.Stop(); // Cannot set duration whilst Particle System is playing
    27.  
    28.             var main = ps.main;
    29.             main.duration = TriggerJumpBoost.BoostDur;
    30.             ps.Play();
    31.             }
    32.  
    33.      
    34.         }
    35.     }
    36. }
    37.  
    I hope it will help something.

    One more thing. Are you changing duration of more than one Particle System/GameObject?
     
    Last edited: Oct 21, 2020