- Home /
Take boolean from other script error
I know there is alot of threats about taking booleans from other scripts, but mine just seem to dont work as it should and i cant find the mistake for myself. I have a flashlight attached to first person controller named flashlight.js
var on : boolean = false;
function Update() {
if (zibintas.paimtas == true) { //will check if true
if(Input.GetKeyDown(KeyCode.F))
on = !on;
if(on)
light.enabled = true;
else if(!on)
light.enabled = false;
}
}
and i have an object named zibintas with script named zibintas.js attached to it :
static var paimtas : boolean = false;
var player = GameObject.FindGameObjectWithTag("Player");
function OnTriggerEnter (player : Collider) {
if(player.tag=="Player") {
if (paimtas == false) {
paimtas = true;
GameObject.Find("zibintas");
Destroy (gameObject);
}
}
}
I need for flashlight to check if zibintas boolean named paimtas == true; and if true, the flashlight should work normaly but if i write the line if (zibintas.paimtas == true) it just doesnt do anything even tho paimtas changes to true;
Answer by craigaw · Jan 09, 2014 at 06:43 AM
You need to define what zibintas is in the flashlight.js script first, otherwise zibintas is just pointing to an empty variable. Probably the easiest way is to link the zibintas object to the flashlight.js in the inspector and then access the paimtas variable.
var on : boolean = false;
var script : zibintas; //this name must exactly match the name of the script
function Update() {
if (script.paimtas == true) { //will check if true
if(Input.GetKeyDown(KeyCode.F))
on = !on;
if(on)
light.enabled = true;
else if(!on)
light.enabled = false;
}
}
Thank you, this worked but also i renamed zibintas.js to be different from object name and because of that it worked perfectly :) Thank you again
Your answer
Follow this Question
Related Questions
Script doesn't find other script 0 Answers
How to display GUI in sequence in trigger 3 Answers
how to know which script you change? 3 Answers
Can someone translate C# to Javascript? 2 Answers
Boolean wont acitvate if the enemy enters a trigger 1 Answer