Light switch (33138)

i have a torch with a point light attached and i use this script for turning it on and off:

function Update() {

if (Input.GetKeyDown(“mouse 0”)) {

if (light.enabled == true)

	light.enabled = false;

	else

	light.enabled = true;

}

}

For a fire effect i added a particle system but how do i make it that they both turn on when i press my left mousebutton and disapear when i release it?

Or just disapear when i press again.
Evantualy i need both so i hope that you could help me.

4 Answers

4

this should work , choose if the game opject have a light component or a particle emmeter or both by chick them

var lightOk = true;

var particleEmitterOk = true;

function Update()

{

if (Input.GetKey("mouse 0")) 

{

	if(lightOk)

	{

	light.enabled = true;

	}

	if(particleEmitterOk)

	{

	particleEmitter.emit = true;

	}

}

else

{

	if(lightOk)

	{

	light.enabled = false;

	}

	if(particleEmitterOk)

	{

	particleEmitter.emit = false;

	}

}

}

i edited the script on this answer try it

you should add the script to the light object and the particle system object

problem no 1 : uncheck the LightOK in the script in the particle system and uncheck the ParticleSystemOK in the script in the light problem no 2 : don't uncheck the Particle Renderer , uncheck the Emit option in the particle emitter i think that was the problem

Try this

function Update() {

if (Input.GetKeyDown(“mouse 0”)) {

if (light.enabled == true)

light.enabled = false;
particleEmitter.emit = false;

else

light.enabled = true;
particleEmitter.emit = true;

}
}

add it to any thing containing a light and a particles , if you want to add it to an objects that contains just a light or just a particles i will give you the code

problem fixed with function Update() { if (Input.GetKeyDown("mouse 0")) { if (light.enabled == true) { light.enabled = false; particleEmitter.emit = false; } else { light.enabled = true; particleEmitter.emit = true; } } } if you want to turn them off by default go to the light component and turn the chick box off , on the particle particleEmitter turn the emit chick box off

sry but it doesn’t work it gives me errors that say:

Assets/ParticleSwitch.js(11,1): BCE0044: expecting }, found ‘else’.

and:

Assets/ParticleSwitch.js(15,3): BCE0044: expecting EOF, found ‘}’.

so i can’t play it is there a solution to these errors?
maybe some mistakes in the script??

it works sort of if you put it like this:

function Update()

{

if (Input.GetKeyDown(“mouse 0”))

{

if (light.enabled == true)

{

light.enabled = false;

particleEmitter.emit = false;

}

else

{

light.enabled = true;

particleEmitter.emit = true;

}
}}

but then still when i attach it to the torch object it says there is no Light attached to it.

only if i put it on the light itself it works but not exactly how i would like it becouse it turns on when i click and off when i click again and i would like that if i release my mouse it turns off.

and the script wont do anything with my particles is that becouse i use a particle system?

becouse when i put the script on that to nothing still happens so did i do something wrong??

oh yeh im putting the story im working on of the game up on deviantart tomorrow i think.

i hope im not bothering since im realy bad at scripting myself and this is my first real game i try.

and if you’re wondering why i want the mouse button like that is becouse the torch is more of a fire staff.