Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Particle Effect Not Playing

Discussion in 'Scripting' started by SamuraiKenshinC, Apr 3, 2020.

  1. SamuraiKenshinC

    SamuraiKenshinC

    Joined:
    Feb 10, 2020
    Posts:
    19
    I don't know if this issue is a script error or not but this seems to be the closest thread to particle issues so I'm still gong post this here. My problem is that though my particle system works fine in the editor an on awake, if I try to play it using "particleSystem.Play()" it doesn't work. The reason it say I don't know if this is a script error or not it because it doesn't give me an error message but all trials and tests point to it potentially being in here (Line 42). Also I checked and the problem most likely isn't the "Destroy(gameObject)" line under it. Any help would be appreciated :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PigEnemyScript : MonoBehaviour
    6. {
    7.     public float runSpeed;
    8.     public float lookSpeed;
    9.     public float health;
    10.  
    11.     private GameObject player;
    12.     public Rigidbody pigRb;
    13.     public ParticleSystem splatParticle;
    14.    
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         player = GameObject.Find("Player");
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         FollowPlayer();
    26.         HealthManager();
    27.     }
    28.  
    29.     void FollowPlayer()
    30.     {
    31.         pigRb.AddForce((player.transform.position - transform.position).normalized * runSpeed);
    32.  
    33.         Vector3 direction = (player.transform.position - transform.position).normalized;
    34.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    35.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * lookSpeed);
    36.     }
    37.  
    38.     void HealthManager()
    39.     {
    40.         if (health < 0)
    41.         {
    42.             splatParticle.Play();
    43.             Destroy(gameObject);
    44.         }
    45.     }
    46.  
    47.     private void OnCollisionEnter(Collision collision)
    48.     {
    49.         if (collision.gameObject.CompareTag("Bullet"))
    50.         {
    51.             health -= 20;
    52.         }
    53.     }
    54. }
    55.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Does line 42 actually execute? Put a Debug.Log() statement before line 42 and see.

    Note that your health would have to be LESS THAN zero, not equal to zero. Is that your problem?
     
  3. SamuraiKenshinC

    SamuraiKenshinC

    Joined:
    Feb 10, 2020
    Posts:
    19
    I've tried a Debug.Log() line and it worked. Also, none of the enemies' health starts at a number that's divisible by 20 to avoid the 0 problem. Thanks for trying though :D
     
  4. ShamusO

    ShamusO

    Joined:
    Jan 12, 2016
    Posts:
    6
    Did you set the public variable splatparticle in the editor? I added it to my "mover" script to see how it would work. I see the attribute added to the game object but when I attempt to select a particle system from the selection circle I don't get any options. I have particle systems in the scene and in prefabs folder.
     

    Attached Files:

  5. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Is splatParticle apparented as child of the gameObject you destroy in the same frame?
     
  6. SamuraiKenshinC

    SamuraiKenshinC

    Joined:
    Feb 10, 2020
    Posts:
    19
    Yeah the effect is public and is set in the editor. Maybe the problem is that the particle itself is a child of a prefab
     
  7. SamuraiKenshinC

    SamuraiKenshinC

    Joined:
    Feb 10, 2020
    Posts:
    19
    It is, but even if I don't destroy the object it doesn't play.