Noob with a noob question.

I am really new to javascript, and if you care could you please tell me what is wrong with this code:

var lightPrefab : Light;
private var OnOff = 1;

function Update () {

if (Input.GetButtonDown ("Fire1"))
{
if (OnOff ==1)
{
lightPrefab.light.intensity = 0;
OnOff = 0;
}

}

else if (Input.GetButtonDown ("Fire1"))
{
if (OnOff == 0)
{
lightPrefab.light.intensity = 1;

OnOff = 1;
}
}
}

I have tried a number of attempts, leaving me with a torch either only disabling or ending up in an error.

I know this is probably simple for most people, but I would like to learn how to do such a thing.

Thanks for any advice.

The first recommendation I have is putting those inner conditional statements into one statement. If they both start with check the Fire1 button, that’s good enough, then when you’ve confirmed that, just flip the state of the light. “OnOff” is a confusing variable name, if you think of it as a bool, then you have “OnOff” is true or false whereas just saying “On” as true or false is more intuitive.

Those aside, what is the error given to you? Are you properly setting the lightPrefab?

I’m shocked at how forgiving JavaScript/UnityScript is on types, it makes it harder for me to read.

I do not really have an error. In game I have a flashlight with a light attached to it (var lightPrefab : Light;). I dropped the light onto that in the editor.

When I go ingame, I press left mouse and the light disappears, and when I press the LBM again nothing happens when the light should show itself again.

try this:

//start with onOff = false = 0
var onOff : boolean = false;

function Update (){
	
	if (Input.GetButtonDown ("Fire1")){
		
		//onOff = true = 1
		if (onOff){
			print ("On");
			//lightPrefab.light.intensity = 1;
			onOff = false;
		}else{
		//onOff = false = 0
			print ("Off");
			//lightPrefab.light.intensity = 0;
			onOff = true;
		}
		
	}
	
}

OK, that is a better explanation.

Here is the problem, your “else if” is ignored, because the first if catches it. Then the first if goes into its inner conditional, which says “if the light is on, turn it off”. You might never reach the inner conditional for else if.

When you do an if/elseif/else they are exclusive, the first conditional to match is executed, then the rest are ignored.

The problem is the If/elseif block, what you’re saying is this:

If you’ve clicked, do A, else if you’ve clicked, do B.

So naturally, you can never do the second else block because it always does A.

if (Input.GetButtonDown ("Fire1"))
{
	if (OnOff == 1)
	{
		lightPrefab.light.intensity = 0;
		OnOff = 0;
	}
	else
	{
		lightPrefab.light.intensity = 1;
		OnOff = 1;
	}
}

But if you want a nicer way of scripting it:

private var IsOn : Boolean = true;

function Update () 
{
	if (Input.GetButtonDown ("Fire1"))
	{
		IsOn = !IsOn; //flips it between true/false
		lightPrefab.light.intensity = IsOn ? 1 : 0; //if IsOn is true, uses "1", otherwise if it's false, use "0"
	}
}

EDIT: damn you JRavey! Good explanation.

EDIT: lol, and rizn. Serves me right for watching Daily Show at the same time and taking my time writing this :stuck_out_tongue:

rizn, thanks, it worked. And JRavey, thanks for your explanation why it did not work.

Now I only need to figure out why it worked, but I can do that on my own.

Thank you.

All of that is unnecessary anyway.

var lightPrefab : Light;

function Update () {
    if (Input.GetButtonDown ("Fire1")) {
        lightPrefab.enabled = !lightPrefab.enabled;
    }
}

–Eric

// Forum code beware

private var curIntensity: int;

curIntensity = 1;

function Update (){
	
	if (Input.GetButtonDown ("Fire1")){
	
	curIntensity = (curIntensity == 1) ? 0 : 1;
        lightPrefab.light.intensity = curIntensity;

	}
	
}

Be a lot simpler and less convoluted if you used a ternary operator.

Ack, fizman beat me to it.

Thanks Eric, I was just thinking that too.

I guess if you needed to keep the light enabled for other reasons, it could just be:
lightPrefab.light.intensity = (lightPrefab.light.intensity == 1) ? 0 : 1;

Nah, in that case you’d do

lightPrefab.intensity = 1-lightPrefab.intensity;

:slight_smile: Make sure it’s 1 or 0 first.

–Eric

Still most of this seems like rocket science to me. I have to learn more Javascript.

Tonyd has written a introduction to scripting which you may find useful: http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)

I was looking for something like that, thanks. :slight_smile: