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?)
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.
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.
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?
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?