- Home /
How to play a particle system?
I have a particle system that i want to play on input. I have made the input part of the code but how do i start this particle system, what script should i use?
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attack : MonoBehaviour
{
[SerializeField]
KeyCode attackKey;
private void FixedUpdate()
{
if (Input.GetKey(attackKey))
{
}
}
}
Answer by trebuH · Feb 13, 2020 at 05:59 PM
Reference your ParticleSystem and use .Play() on it. Like: ps.Play()
Thanks for your answering, but i recieve a error saying "Object reference not set to an instance of an object"
Answer by maormenashe · Feb 13, 2020 at 06:04 PM
The Attack script is attached to the particle or a different GameObject? If it's not attached to the particle GameObject itself you'd have to pass a reference of the ParticleSystem via inspector or find the GameObject via scipt and get the ParticleSystem component. Once you have a reference of the ParticleSystem just use the Play method.
Example Attack Script not attached:
ParticleSystem ps = GameObject.Find("ParticaleSystem-Object-Name").GetComponent<ParticleSystem>();
ps.Play();
Example Attack Script attached:
ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Play();
Your answer
![](https://koobas.hobune.stream/wayback/20220612223255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
turning particle systems off 2 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
OpenXR Continuous Turn and Movement not working 0 Answers
IOS object touch, collider 1 Answer
Is it good to specify what Actions of the Input Manager do on several Scripts? 1 Answer