Turning off and turning on these rays


I’m trying to turn on and off these rays when I click left mouse button. If I’m pressing the left mouse button I want to mantain enabled the rays and if I’m not clicking with the left mouse button I want to turn off the rays. The code I’m trying to reuse comes from:

And the main script is:

	This script is placed in public domain. The author takes no responsibility for any possible harm.
	Contributed by Jonathan Czeck
using UnityEngine;
using System.Collections;

public class LightningBolt : MonoBehaviour
	public Transform target;
	public int zigs = 100;
	public float speed = 1f;
	public float scale = 1f;
	public Light startLight;
	public Light endLight;
	public Perlin noise;
//	Perlin noise;
	float oneOverZigs;
	private Particle[] particles;
	public int w;
	void Start()
		//if((Input.GetButtonDown ("Fire1") )&&(w==3))
		oneOverZigs = 1f / (float)zigs;
		particleEmitter.emit = false;

		particles = particleEmitter.particles;
	void Update ()
		//if((Input.GetButtonDown ("Fire1") )&&(w==3))
		if (noise == null)
			noise = new Perlin();
		float timex = Time.time * speed * 0.1365143f;
		float timey = Time.time * speed * 1.21688f;
		float timez = Time.time * speed * 2.5564f;
		for (int i=0; i < particles.Length; i++)
			Vector3 position = Vector3.Lerp(transform.position, target.position, oneOverZigs * (float)i);
			Vector3 offset = new Vector3(noise.Noise(timex + position.x, timex + position.y, timex + position.z),
										noise.Noise(timey + position.x, timey + position.y, timey + position.z),
										noise.Noise(timez + position.x, timez + position.y, timez + position.z));
			position += (offset * scale * ((float)i * oneOverZigs));
			particles*.position = position;*

_ particles*.color = Color.white;_
_ particles.energy = 1f;

* particleEmitter.particles = particles;*

* if (particleEmitter.particleCount >= 2)*
* {*
* if (startLight)*
* startLight.transform.position = particles[0].position;*
* if (endLight)*
* endLight.transform.position = particles[particles.Length - 1].position;*
* }*
* //}*

*} *
What is commented is what I’ve tried till now. I only emit one ray and the ray stays at one position but doesn’t go with me…So I don’t know what to do. The idea is to enable and desable this script but I don’t know If I’m going in the right direction.
The final purpose is to make some electric rays going to the same point, from a point to another point.
Thank you in advance.

This script from the example make the emitter fire un shot of particles and gather them all inside an array at start. Then in update, their positions are slightly shaken to produce the electric effect.

Now, you apparently want to make those particles disappear and reappear. Lucky for you, this can be easily done by toggling the particle renderer component (GetComponent< ParticleRenderer >().enable = …). Note that you will need to test if the renderer is enabled in update. No need to apply noise on something invisible isn’t it ?

As for the input now. GetButton down will be true for only one frame, the closest one to th moment you pressed the button. Which means, if you execute the code in update only when GetButtonDown returns true, the code will be executed once. Try this instead :

ParticleRenderer r;
void Start()
    // Keep the tutorial stuff
    r = GetComponent< ParticleRenderer >();
void Update()
    if((Input.GetButtonDown ("Fire1") )&&(w==3)) 
        r.enabled = !r.enabled;

    if( r.enabled )
        // Noise code

NOW IT WORKS: I modified the Update() loop function and the enable activation with the registry of an input through the mouse. now when I’m pressing the left mouse button the electric rays are turned on and turned off when I stop the pressure over the left mouse button. So thanks Bérenger Mantoue for your help, because I didn’t know how to get this component in C# (and in other languages in general). So here is the final code where only changes the Update function() because the code of before is right (and the useless of the w varaiable, which was eliminated too in this correction):

    void Update ()
		r.enabled=true ;
        if( r.enabled==true)
                //noise code