I have a simulation using the idea of a hot potato and when the lifetime of the potato expires the particle explosion should play; however, it does not. I confirmed that when ‘Play on Awake’ the particle does play on the attached object but for some reason, it will not play within the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PotatoController : MonoBehaviour
{
public AudioClip explodeSound;
public ParticleSystem explosionParticle;
private float lifeTime;
private AudioSource playerAudio;
// Start is called before the first frame update
void Start()
{
// Randomly select life of potato from 5 to 10 seconds
lifeTime = Random.Range(5.0f, 10.0f);
playerAudio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (lifeTime > 0)
{
lifeTime -= Time.deltaTime;
}
// Once lifetime is depleted, use explosion particle, play audio and destroy potato
if (lifeTime <= 0)
{
explosionParticle.Play();
playerAudio.PlayOneShot(explodeSound, 1.0f);
Destroy(gameObject, explosionParticle.main.duration);
}
}
}
What could be happening is your update is calling explosionParticle.Play() repeatedly, depending on how long the particle is set to play for. Thus, each additional call is messing up the play of the particle.
When I tried a small test, the particle didn’t play when I let Update call play repeatedly. To adjust for it, I just checked if the particle was already playing and didn’t trigger it again.
It might not be enough to do it this way, but try this.
No luck. I also tried just putting the explosionParticle.Play() directly in the Start() method and it still didn’t play. I am really confused as to why it will not play.
Then the next thing to look for is. Does it play in editor? Is it hidden behind anything when it triggers? Make sure it isn’t getting destroyed to quickly. Comment out destroy and set the particle to loop just to test it. See what happens.
Kurt’s post below added a good point. Since play on awake works on the particle, maybe I assumed you had the script attached to the gameobject correctly and everything else was working. (The sfx was playing and the gameobject was getting destroyed). But then, I shouldn’t assume that. So, does the other parts of the code even run?
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
When in doubt, print it out!™
Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.
You must find a way to get the information you need in order to reason about what the problem is.
I’m gonna guess line 33 is executing on frame one with a duration of zero and wiping out everything, or else the script simply isn’t properly configured to run.
I simply put the explosion on the Start() method with logs around it and still nothing. The logs output just fine but both show that it isn’t playing. Again if I leave ‘Play on Awake’ no issues with the particle system on the object.
void Start()
{
// Randomly select life of potato from 5 to 10 seconds
lifeTime = Random.Range(5.0f, 10.0f);
playerAudio = GetComponent<AudioSource>();
Debug.Log("Explosion Playing: " + explosionParticle.isPlaying);
explosionParticle.Play();
Debug.Log("Explosion Playing: " + explosionParticle.isPlaying);
}