- Home /
Play particle effect when Space Bar is pressed
I'm currently working a "Cannon Defense" game and I've been searching across Unity Answers and Google for the exactly same subject but there's nothing I couldn't find at all. I actually purchased an explosion+gun particle asset that I needed for my project. Could anyone help me with this? All I'm asking is a script that plays a particle effect whenever I press the Space Bar.
Here's my script:
Thanks
Answer by clunk47 · Nov 25, 2013 at 12:25 AM
EDIT: (User turns out not to be using a particle system, but HTSpriteSheet.cs).
Just add a new script to the GameObject that HTSpriteSheet is attached to, and enable / disable the script like so:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
bool animate = false;
HTSpriteSheet htss;
void Start()
{
htss = GetComponent<HTSpriteSheet>();
}
void Update ()
{
htss.enabled = animate;
if(Input.GetKeyDown (KeyCode.Space))
animate = !animate;
}
}
Be sure your script's name is the same as the class name.
Just before I try it out, what's a "Shuriken system"? (Never heared of this before)
Try the script and you'll know if you're using it lol. http://docs.unity3d.com/Documentation/Components/class-ParticleSystem.html
lol ok so if you're using Legacy particles, use the code in my answer. If you're using Shuriken, use the same code, except change
particleEmitter.emit = emit;
TO
particleSystem.enableEmission = emit;
Since you're accessing the component directly, this means you attach the script to the object with the particleSystem component attached, which is the particle system itself. If you're using Shuriken, you'll just have Particle System component. If you're using Legacy, you'll see Ellipsoid Particle Emitter, Particle Animator, and Particle Renderer components attached to the object.
Look in the script where it reads
public class Example : $$anonymous$$onoBehaviour
The script's name needs to match the class's name. Either name your script 'Example', or change the name of the class inside the script to match what you have your script named as.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Enable/Disable shader on script 2 Answers
Particles do not appear on play but do on emit 0 Answers
How to create particle system on a collision? 1 Answer
Particle follow object 1 Answer