Search Unity

Trouble getting particle effect to work

Discussion in 'Scripting' started by Delloy, Sep 13, 2019.

  1. Delloy

    Delloy

    Joined:
    Aug 25, 2019
    Posts:
    2
    Hi,

    I am new to unity/coding and I am trying to build my first game. I want the player to die when the enemy touches the player, so far this bit works but I am not getting any particles emit so the player just vanishes once the enemy touches it.



    Code (csharp):
    1.  
    2.  
    3. public GameObject splatter;
    4.  
    5.  
    6.  
    7. void OnTriggerEnter(Collider other)
    8.     {
    9.    
    10.         if (other.gameObject.tag == "Enemy")
    11.  
    12.         {
    13.             Instantiate(splatter, gameObject.transform.position, Quaternion.identity);
    14.        
    15.             Destroy(gameObject);
    16.         }
    17.     }
    18.  

    I am a bit stumped on what to do next :/
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,099
    Everything above looks 100% reasonable. Is your particle system prefab set correctly: play on awake, etc.

    Try this: make a new "splatter" prefab, name it "test cube" or something, and just put a single cube (or square sprite) in that prefab and save it off.

    Now in your scene change the inspector field that has 'splatter' so that it refers to this new "test cube" object and run your game.

    Do you see the test cube appear when you die? Is it even in the scene at the point where you die? etc.

    ALSO: you can put this line right beside your Destroy() line above:

    Code (csharp):
    1. Debug.Break();
    And Unity will pause the Editor the instant you hit that line, so you can look in your scene and see if your particle system is there, if it's not there, or if maybe it is somewhere far away.

    The above process is basically just the kinda troubleshooting you should be doing whenver something misbehaves, as it will be something to be very familiar with.
     
    Delloy likes this.
  3. Delloy

    Delloy

    Joined:
    Aug 25, 2019
    Posts:
    2
    Thank you Kurt, turns out I had 'Play on awake' disabled, what a muppet. Thanks for your help again
     
    Kurt-Dekker likes this.
  4. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    669
    I think you should delay Destroy(gameObject);