Search Unity

How to start and play a Particle effect on mouse click?

Discussion in 'Scripting' started by left4kill5, Sep 20, 2020.

  1. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Hello! I developed a muzzle flash for my pistol. For some reason, I cannot reference it on my script. I have also tried:
    GameObject.find("MuzzleFlash").GetComponent<ParticleSystem>().Play();

    This does not work either. If anyone can tell, I will appreciate it!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ShootingPlayer : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private Animator animator;
    9.  
    10.  
    11.     public Camera cam;
    12.     public int Damage = 10;
    13.     public ParticleSystem MuzzleFlash;
    14.     [SerializeField]
    15.     private LayerMask enemy;
    16.  
    17.  
    18.     void start()
    19.     {
    20.         animator.SetTrigger("Idle");
    21.         if (cam == null)
    22.         {
    23.             Debug.LogError("No camera referenced");
    24.             this.enabled = false;
    25.         }
    26.     }
    27.  
    28.  
    29.     void Update()
    30.     {
    31.        
    32.         animator = GetComponent<Animator>();
    33.        
    34.         if (Input.GetButtonDown("Fire1"))
    35.         {
    36.             MuzzleFlash.Play(); //Cannot play without referencing it.
    37.  
    38.             Shoot();
    39.             animator.SetTrigger("Shooting");
    40.         }
    41.  
    42.  
    43.     }
    44.     void Shoot()
    45.     {
    46.        
    47.         Debug.Log("Shot bullet");
    48.         RaycastHit hit;
    49.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, Mathf.Infinity, enemy))
    50.         {
    51.             if (hit.collider.tag == "enemy")
    52.             {
    53.                 hit.collider.gameObject.GetComponent<Dmg>().TakeDamage(20);
    54.  
    55.             }
    56.         }
    57.  
    58.  
    59.     }
    60.  
    61.  
    62. }  
    63.  
    upload_2020-9-20_14-44-2.png <---- Can't reference the Particle effect.
     
  2. Deleted User

    Deleted User

    Guest

    muzzleFlash being a Particle System, you cannot use GameObject to find it.

    Not tested:
    Code (CSharp):
    1. private ParticleSystem muzzleFlash;
    2.  
    3. private void Start()
    4. {
    5.     muzzleFlash = Find("MuzzleFlash").GetComponent<ParticleSystem>(); ///not tested
    6. }
    7.  
    8. private void Update()
    9. {
    10.     muzzleFlash.Play();
    11. }
     
  3. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    This doesn't seem to work, I think "find" isn't actually a real command.
    Find("MuzzleFlash").GetComponent<ParticleSystem>();
     
    Last edited: Sep 20, 2020
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    1. You almost definitely have an unresolved compile error in one or more scripts because I do not see this field in your inspector screenshot:
      public ParticleSystem MuzzleFlash;
      , nor do I see the Animator field. Check your console for errors.
    2. Your Start() function is misspelled as start(), so it will not run.
    3. If you have a public field that you have assigned in the inspector for the ParticleSystem, you don't need to be using GameObject.Find or anything like that. You will already have the reference.
     
  5. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Oh, Thank you, I did not notice that. It seems that I have no errors in the console, however, but I still do not see the MuzzleFlash field in the inspector. I have corrected my start typo. I am not sure what you mean in the public field that I assigned to the particle system.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    In the screenshot you're showing a script called "Shooting", and your code is "ShootingPlayer". These are two different scripts. Add the correct script to your object.
     
  7. Ta-SeenUNITY25

    Ta-SeenUNITY25

    Joined:
    May 29, 2021
    Posts:
    3
    There are some mistakes in your code, here's how to make the particle start anyway:

    [using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class ShootingPlayer : MonoBehaviour
    {

    public ParticleSystem muzzleflash; //remember to insert your muzzle flash in the inspector


    if (Input.GetMouseButtonDown(0) //starts on left click
    {
    muzzleflash.Start();

    }
    else
    {
    muzzleflash.Stop();
    }

    }