Search Unity

Particles Not Playing

Discussion in 'Scripting' started by R-ty_dragon1, Jun 27, 2022.

  1. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    51
    Hello!

    I am making some code to play an explosion when an object collides.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Timer : MonoBehaviour
    6. {
    7.     public ParticleSystem explosion;
    8.  
    9.     void Start()
    10.     {
    11.         explosion.GetComponent<ParticleSystem>();
    12.     }
    13.  
    14.     void OnCollisionEnter(Collision col)
    15.     {
    16.         if (col.gameObject.tag == "Obstacle")
    17.         {
    18.             Time.timeScale = 0.3f;
    19.             StartCoroutine(SloMo());
    20.         }    
    21.     }
    22.  
    23.     IEnumerator SloMo()
    24.     {
    25.         explosion.Play();
    26.         yield return new WaitForSeconds(1);
    27.         gameOver.SetActive(true);
    28.         takingAway = false;
    29.         Time.timeScale = 0f;
    30.     }
    31. }
    32.  
    The explosion doesn't play but the game does go into slow motion like it is supposed to. The code stops and tries to play the particle but doesn't and pauses the code and everything after the
    Code (CSharp):
    1. explosion.Play();
    doesn't run.
     
  2. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Doenst Time.timeScale = 0f pause the game?

    From a google search;

    "The most convenient method for pausing the game in Unity is by setting the game’s time scale to zero (Time.timeScale = 0), which effectively pauses all time based operations including movement, physics and animation. Setting the time scale to one again will return the game to its normal speed"
     
  3. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    51
    Yes it does - I play the particle and then wait 1 second and then show a game over popup which pauses the game behind it. The particle doesn't play though and it doesn't even get to that line of code. I assume the code is trying to play the particle and is waiting for the particle to play but it isn't, thus stopping the script from playing
     
  4. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    131
    Are you seeing any errors in the console when you play?

    Something tells me you're probably getting a Null Reference error due to the "explosion" particle system not being properly referenced.

    I see you have "explosion" as a public ParticleSystem reference, for example, yet you're also attempting to acquire the reference in Start (which seems redundant; since it's a public reference, why not just drag-and-drop it in the Inspector?). I strongly suspect that's what's causing things to break.

    Try getting rid of the Start method all-together and just dragging-and-dropping your "explosion" reference in the Inspector.
     
    richardkettlewell likes this.
  5. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    51
    Thanks! I did what you said and it worked. I got all mixed up using bits from many different codes i had found with the same issue. I removed a component attached to the particle to destroy once it is played as it automatically played at the beggining and then destroyed itself
     
  6. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    51
    I was just wondering aswell how would I change the position of the particle to a position of a game object?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697