- Home /
[Newbie] Particle System not playing, C# (unity 5.5)
Hello everyone :)
RECAP VERSION : I want a game object to play a particle system once and disappear when it touches another GameObject with the tag "cube".
DETAILED VERSION : I have a canon with which you knock cubes of platforms. The cannonball should work like a bomb in that, when it touches a gameObject with the tag "cube", it plays a particle system (like an explosion effect) then disappears. I don't need the "explosion" to have any kind of physical force, it is just there to make the disappearing look pretty.
Everything works well except the particle system. I've looked on the Internet, of course, but I can't get it to work. I honestly can't tell you everything that I've tried. I did try with OnTriggerEnter, with different lines of code, with a getComponent, I've moved the lines a lot, created functions and calling them, removing and adding things... Everything that seemed logical and possible with my very limited understanding of Unity and scripting. I didn't get that much of compile errors. It just doesn't play. Here is the script I have now (no compile error but still doesn't work):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBehaviour : MonoBehaviour {
public ParticleSystem ps;
void OnCollisionEnter (Collision ColBallCube){
if (ColBallCube.gameObject.tag == "cube") {
//Destroy (gameObject, 0.5f);
ParticleSystem.EmissionModule em = ps.emission;
em.enabled = true;
}
}
}
(just to be sure, I've toggled the destroy part to commentary line).
Obviously I'm doing something wrong or missing something. It is also possible that there's a problem with the particle system settings (looping ? Play on Awake ? etc.).
Does someone has an idea of how to solve this? Thanks for any kind of help.
Have a good day :)
Answer by Ledifen · Aug 09, 2017 at 07:35 PM
After a lot of failed attempts, I finally managed to make it work properly. With the advice of @Y00, this is what I have (and it works) :
1 empty object (emptyHoldingPs) with the particle system;
1 game object sphere (physcisBall) that is visually the "cannon ball", but has no physical existence except the look (basically just a mesh);
1 empty object (cannonBall) with a rigidbody, the sphere collider and the script.
The first two are attached as child of the third, so the empty object "CannonBall" is what I throw with my canon.
public GameObject emptyHoldingPs; //drag the empty Obj with the particle system component
public GameObject physicsBall; //drag the game obj with the mesh
private bool hasCollided = false;
ParticleSystem ballParticleSystem;
void Start(){
ballParticleSystem = emptyHoldingPs.GetComponent<ParticleSystem> ();
ParticleSystem.EmissionModule em = ballParticleSystem.emission;
em.enabled = false;
}
void OnCollisionEnter (Collision ColBall){
if (hasCollided == false){
ParticleSystem.EmissionModule em = ballParticleSystem.emission;
em.enabled = true;
ballParticleSystem.Play();
hasCollided = true;
Destroy (physicsBall, 0f); // I destroy the mesh right away
Destroy (gameObject, 0.5f); // I destroy the entire cannon ball with a delay so the particle system has the time to play completely once.
} else {
hasCollided = false;
}
}
To deal with a potential collision happening between the first collision and the "Destroy (gameObject, 0.5f);", I'v used a boolean "hasCollided". The part with the boolean is untested, but the rest works.
Answer by Y00 · Aug 03, 2017 at 01:06 PM
public ParticleSystem MyParticle; //Specify the link in the inspector
if (ColBallCube.gameObject.tag == "cube") MyParticle.Play();
.......
MyParticle.Stop();
or place your ParticleSystem on any GameObject and: gObj.SetActive(true); gObj.SetActive(false);
Okay thanks for the answer :)
If I choose the first solution, where do I put "$$anonymous$$yParticle.Stop();". In your lines of code, what does the line of dot represent? I've already tried something with Play and Stop, but I don't know how to handle the rest of the code concerning ParticleSystem.
For now, that's what I have (didn't put the Stop since I'm not sure where to put it):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBehaviour : $$anonymous$$onoBehaviour {
public ParticleSystem ps;
void OnCollisionEnter (Collision ColBallCube){
if (ColBallCube.gameObject.tag == "cube") {
//Destroy (gameObject, 0.5f);
ps.Play();
ParticleSystem.Emission$$anonymous$$odule em = ps.emission;
em.enabled = true;
//ps.Stop ();
}
}
}
I'm sure I'm missing something here, and I'm pretty sure it's simple, but for now it doesn't click in my $$anonymous$$d... Sorry :/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBehaviour : $$anonymous$$onoBehaviour {
public ParticleSystem ps;
void OnCollisionEnter (Collision ColBallCube){
if (ColBallCube.gameObject.tag == "cube") {
ps.Play();
StartCoroutine(Cor());
}
}
IEnumerator Cor()
{
yield return new WaitForSeconds(1);
ps.Stop();
Destroy (gameObject);
}
}
or simple: if (ColBallCube.gameObject.tag == "cube") {
ps.Play(); Destroy (gameObject, 1f); }
Again, thank you for the time you spend on this question. :)
Unfortunately, it doesn't work (I've tried both solutions). There's no compile error, but it doesn't play (but the destroy works fine).
I don't know, maybe it has to do with the particle system in itself. I've used the default settings, then changed the "play on awake" and "looping" booleans. It just wouldn't play.
"looping" - true, "play on awake" - false ?
Is it displayed in the scene window?
You can see the work with the particles in my project. https://yadi.sk/d/5lsDUfkA3LiCBJ
for example Game->Grid->AirPlane->AirPlaneForRotate->ParticleWater looks like your situation. However, this particle is displayed not by collision, but by pressing the interface button (Similar code in the file Assets\Scripts\Airplane.cs method IWatering())
I do not speak English)) (use Google translator)
Thanks a lot for all the help you've provided me.
I see that your scripts are very complete and maybe a tad bit too complex for me to understand. For example, I don't know what "Abilities", "WateringAbility", "IsUse" and "TimeUse" represent so I'm struggling with that.
Anyway, I'll try finding an other way of doing this. I'll stop bothering you.
Again, thank you for your time and help, it's very much appreciated. :)
Your answer
Follow this Question
Related Questions
How to activate gameObjects based on a boolean from another scene? 1 Answer
Using AirBrakes on mobile device with Standard Assets AircraftController 0 Answers
Hello, could I get some help with my inv system? 1 Answer
Built project, now scripts are missing. 2 Answers
Rotating a cube horizontal axis 1 Answer