- Home /
light switch
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.
Answer by Subhi · Dec 16, 2011 at 12:50 PM
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;
}
}
}
it works! but only not for my particles could this be becouse is use a particle system ins$$anonymous$$d of an emitter?
you should add the script to the light object and the particle system object
problem no 1 : uncheck the LightO$$anonymous$$ in the script in the particle system and uncheck the ParticleSystemO$$anonymous$$ 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
thank you for everything it works exacly how i wanted it!!
Answer by Subhi · Dec 15, 2011 at 02:18 PM
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;
} }
so i just add it to the object containing the light and the particles or the light with the particles??
BTW thank you for your time!
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
oke but it gives me 2 errors
Assets/on and off.js(11,1): BCE0044: expecting }, found 'else'.
and
Assets/on and off.js(15,3): BCE0044: expecting EOF, found '}'.
can that be fixed also how do i set both the light and the particles to be off by default?
oh and i called the script on and off.
problem fixed with
function Update()
{
if (Input.Get$$anonymous$$eyDown("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
Answer by artsdcs · Dec 15, 2011 at 06:07 PM
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??
Answer by artsdcs · Dec 16, 2011 at 01:33 AM
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Does anyone know of some good scripting tutorials? 2 Answers
Flashlight shadow problem 0 Answers
create flashing light effect for ambulance 1 Answer
Problem with ligths! 0 Answers