- Home /
Can someone help me fix my Javascript for Flickering Light?
This is the code i have so far.
#pragma strict
function Start ()
{
var FlashingLight : Light;
FlashingLight.enabled = false;
}
function FixedUpdate ()
{
var RandomNumber = Random.value;
;
if(RandomNumber<=.7)
{
FlashingLight.enabled = true;
FlashingLight.enabled=false;
}
}
If someone could let me know what parts I need to fix, I would really appreciate it!
Right now, the compiler error is telling me that:
Assets/iTween/FlashingLight.js(16,23): BCE0020: An instance of type 'UnityEngine.Behaviour' is required to access non static member 'enabled'.
If someone could explain that to me too, that would be great! Sorry about this!
Next time you post an answer/question/comment with code in it, please use the 101/010 button and make sure it's formatted properly. I edited your post this time for you.
Also, "I have not slept in days" isn't a good tag to use for your posts. I removed that.
Answer by madstk1 · Mar 27, 2014 at 07:44 PM
My best guess is, to remove "FlashingLight.enabled = false;", and put "FlashingLight.active = false;"
(This is untested)
Answer by James_DollaSign · Mar 27, 2014 at 08:36 PM
I appreciate your feedback on this!
I tried this but for some reason this gave me:
Assets/iTween/FlashingLight.js(16,23): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'active'.
you should change Random.value;
to read something like Random.Range(0.0f, 1.0f );
for your first hurdle.
Not that its doing any harm, as compiler puts them there for you anyway, but yo might as well remove your semicolon at line 14 too for posterity.
As for the flicker, you may find it is happening to fast to register even in FixedUpdate(), firstly, move it into late update and then look into normalising the 0-1 range you are getting from Random.Range(float, float); like so..
var myResultFromRandomRange : float = Random.Range(0.0f, 1.0f);
var myClampedRandomRange = $$anonymous$$athf.Clamp(myResultFromRandomRange ,0.1f, 0.8f);
then afterward you would need a call to Update your flashing light with that value... so...
FlashingLight.intensity = myClampedRandomRange;
Unfortuantely, I too have had a long day,so this untested, but the logic is there. Take care bud Gruffy
Answer by The Stick · May 05, 2014 at 06:57 PM
I'd imagine you're getting the error because your FlashingLight
variable is being defined within your Start
function. If you define it in there, it won't be remembered when Update
rolls around.
Move your variable declaration outside of Start
.
(I know nothing of JavaScript, but I still think this might be the reason why.)
Additionally, if this script's being placed on the same GameObject with the light on it, I'd suggest you do something along the lines of:
FlashingLight = gameObject.GetComponent(Light);
This would help prevent any errors revolving around not having a Light to modify.
Answer by OrionGamesInc · Feb 09, 2016 at 09:29 AM
I found, with a bunch of looking around, a really useful script. It uses different methods of creating a flicker, either random or "pulsing" with many uses. You can attach the script to any light in the scene and change the values using the interface. This is what I use (javascript):
// Properties
var waveFunction : String = "sin"; // possible values: sin, tri(angle), sqr(square), saw(tooth), inv(verted sawtooth), noise (random)
var base : float = 0.0; // start
var amplitude : float = 1.0; // amplitude of the wave
var phase : float = 0.0; // start point inside on wave cycle
var frequency : float = 0.5; // cycle frequency per second
// Keep a copy of the original color
private var originalColor : Color;
// Store the original color
function Start () {
originalColor = GetComponent(Light).color;
}
function Update () {
var light : Light = GetComponent(Light);
light.color = originalColor * (EvalWave());
}
function EvalWave () {
var x : float = (Time.time + phase)*frequency;
var y : float;
x = x - Mathf.Floor(x); // normalized value (0..1)
if (waveFunction=="sin") {
y = Mathf.Sin(x*2*Mathf.PI);
}
else if (waveFunction=="tri") {
if (x < 0.5)
y = 4.0 * x - 1.0;
else
y = -4.0 * x + 3.0;
}
else if (waveFunction=="sqr") {
if (x < 0.5)
y = 1.0;
else
y = -1.0;
}
else if (waveFunction=="saw") {
y = x;
}
else if (waveFunction=="inv") {
y = 1.0 - x;
}
else if (waveFunction=="noise") {
y = 1 - (Random.value*2);
}
else {
y = 1.0;
}
return (y*amplitude)+base;
}
Answer by mitchmeyer1 · Aug 09, 2018 at 01:52 PM
You are very close!
3 problems:
1) like The Stick said: you need to move the var FlashingLight outside of the start method. you need to make an 'instance variable' but putting it above the start method, not inside of it. Also you never assign the FlashingLight object to anything. You simply instantiate it, you do not add the component to the gameobject you are on, and you dont look it up using GetComponent() or anything. This Flashing Light object isnt in the game.
2) You do not have an 'else' in you if statement with the random number. Add that
3) (might not be an issue if the Light component is what does the rendering on the light) Simply disabling the Light script may not disable the visuals of that object. You need to do one of 2 things, instead of FlashingLight.enabled = false, you can do FlashingLight.gameobject.SetActive(false), or you need to make FlashingLight the SpriteRenderer or whatever the visual component is! So this second point may not change if the 'Light' component is the thing that renders the image.
Light flashingLight; //Instantiate the variable outside functions so that all functions can use it
function Awake ()
{
flashingLight = gameObject.GetComponent<Light>(); //Assign the variable in awake to the component attached to this game object. If you did not add a light to this game object, add it here
FlashingLight.enabled = false;
}
function FixedUpdate ()
{
var RandomNumber = Random.value;
;
if(RandomNumber<=.7)
{
FlashingLight.enabled = true;
FlashingLight.enabled=false;
}
}