Javascript flashlight problem

got scripts? Need scripts?
Post Reply
Shihonoryu
Posts: 255
Joined: Sun Dec 12, 2010 7:30 am

Javascript flashlight problem

Post 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
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Javascript flashlight problem

Post 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.
The indelible lord of tl;dr
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Javascript flashlight problem

Post 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
Image

Image
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Javascript flashlight problem

Post 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.
Shihonoryu
Posts: 255
Joined: Sun Dec 12, 2010 7:30 am

Re: Javascript flashlight problem

Post 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;
	}	
	


}


Post Reply

Return to “Scripting/Coding”