Page 1 of 1

Javascript flashlight problem

Posted: Mon Apr 16, 2012 7:03 pm
by Shihonoryu
Here are the errors:


Four of these:

Code: Select all

Assets/Custom Scripts/Flashlight.js(9,42): BCE0005: Unknown identifier: 'Lighton'.
Here is the code:

Code: Select all

function Start () {
var Lighton = false;
}

function Update () {
if (Input.GetButtonDown("Flashlight") && Lighton == false)
	{
	light.intensity = 2;
	
	Lighton = true;

	}
if (Input.GetButtonDown( "Flashlight" ) && Lighton == true )
	{
	light.intensity = 0;
	
	Lighton = false;
	}	
	


}



im at a loss

Re: Javascript flashlight problem

Posted: Mon Apr 16, 2012 9:49 pm
by Jackolantern
While I am completely unfamiliar with Unity 3D, I am familiar with JS, and JS has function scope. That means that if you set a variable in a function, once that function returns (ends), the function is popped off the stack and is deleted. So you are creating the lighton variable in the start function and then using it in the update function. In the update function, the lighton variable does not exist. You will need to create the variable in whatever function you want to use it, go up a level higher and either make it global if it is extremely important or add it to a higher scope, or create a closure.

While I don't know Unity, I am assuming they don't break the fundamental rules of JS or C# in their scripts, so that must be the problem.

Re: Javascript flashlight problem

Posted: Mon Apr 16, 2012 9:52 pm
by SpiritWebb
After looking at your script more, get rid of the function start and at the top use:

Code: Select all

public var Lighton : boolean = false;
That should get rid of the errors

Re: Javascript flashlight problem

Posted: Mon Apr 16, 2012 10:07 pm
by Xaleph
meh... the Start() function is mandatory in Unity I believe. That`s the initializer or bootstrap for that particular class.. However, what he should do is store the lighton var above it, making it class scope ( script scope really..) or even better, write the class itself. However, i doubt thats`s not needed.

So in short, remove var lighton from the Start() function and declare it above the Start() function.

Re: Javascript flashlight problem

Posted: Tue Apr 17, 2012 12:02 am
by Shihonoryu
Changed some code around, and now i have no errors.

But now nothing happends when i press F, it actually goes into the wrong if statement but still does not do anything to the light., i know by using debug.log, it writes 2 to the console.

Code: Select all

#pragma strict


var Lighton : boolean = false;
function Start () {
var light : Light;


}

function Update () {

if (Input.GetButtonDown("Flashlight") && Lighton == false)
	{
	Debug.Log("1");
	light.intensity = 1.5;
	
	Lighton = true;

	}
if (Input.GetButtonDown( "Flashlight" ) && Lighton == true )
	{
	
	Debug.Log("2");
	light.intensity = 0;
	
	Lighton = false;
	}	
	


}