- Home /
Why has a script I created altered every instance of an object?
I was trying to make some lighting effects. For lack of a more immediate solution (other than just copying and pasting online, I'm trying to figure out some problems on my own to learn), I wrote the following script for a strobe light effect.
using UnityEngine;
using System.Collections;
public class LightStrobeEffect : MonoBehaviour {
public float MinimumIntensity = 0.0f;
public float MaximumIntensity = 1.5f;
public int SwitchTimer = 10;
private bool lightOn = true;
private int timer = 0;
// Use this for initialization
void Start () {
this.light.intensity = MaximumIntensity;
}
// Update is called once per frame
void Update () {
timer++;
if (timer % SwitchTimer == 0) {
if (lightOn) {
this.light.intensity = MinimumIntensity;
lightOn = false;
} else {
this.light.intensity = MaximumIntensity;
lightOn = true;
}
timer = 0;
} else {
// I don't know of anything that would go here, but it's available
// just in case future updates require it.
}
}
}
Essentially, what the code does it this: three new variables are exposed in the editor. MinimumIntensity, MaximumIntensity, and SwitchTimer, with default values assigned for each. The first two are self-explanatory, and SwithTimer determines how many frames will pass before switching from on to off or vice-versa. The code simply counts up each frame and checks if the value is a multiple of SwitchTimer via a modulus. If so, switch things around.
I added the script to a light and it worked. I was expecting some quirk or coding error to cause revisions to happen, but everything seemed fine right away. Except that the strobe effect has been applied to the other point lights in my scene as well. I double-checked and the script has not been added to the entities themselves, only to the one light I was testing on. But the strobe effect continues to exist on every light. This happens when I run the scene in the editor and when I build and run as a stand-alone executable.
So the question is: why is this happening and what can I do to fix it?
tl;dr: I made a scripted light effect and applied it to a single light but it affects every light; why?
Update: I seem to have narrowed the problem a little bit. I added a new point light to the world and this one does not seem to be affected. The light that IS affected also has a custom script on it. I am attaching it below:
using UnityEngine;
using System.Collections;
public class LightToggle : MonoBehaviour {
private bool activated = true;
private float oldIntensity;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void ToggleLight () {
if (activated) {
activated = false;
oldIntensity = light.intensity;
light.intensity = 0.0f;
} else {
activated = true;
light.intensity = oldIntensity;
}
}
}
The trigger that toggles the light has the following script:
using UnityEngine;
using System.Collections;
public class TriggerToggleLight : MonoBehaviour {
public LightToggle LightToToggle;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) {
LightToToggle.ToggleLight();
}
}
So if it is a matter of how I am programming my scripts, what am I doing wrong and how can I avoid it in the future?
Are those lights prefabs? $$anonymous$$y only explanation is, that they´re prefabs and you hit the "apply" button in the inspector. That would change all objects instantiated from that prefab.
No, I haven't done any work with prefabs. Each light is created via the "GameObject->Create Other->" menu. I thought I may have accidentally done that as well, but I ensured that the script is not active on any light except the one that I intended to test it with.
Answer by TrueKam · May 22, 2014 at 03:04 PM
There wasn't a problem. Apparently the interaction of the lights on a checkered background (using the prototyping textures from the beta sample assets) created the optical illusion that one light was changing as the other was changing. By making the lights contrasting colors and changing the range to be much shorter and out of the area that the lights could "interact" in the middle, the lack of a problem was apparent.
Sorry for the trouble, everyone. Please carry on, I'll see myself out now. ~ TrueKam
Answer by tw1st3d · May 22, 2014 at 12:28 PM
I really wanna say it's because you're using
this.light
If this script is set to a light object, use
gameObject.light
If not, create a
public GameObject eLight
and use
this.eLight.light
I tried replacing the "this" keywords with "gameObject" but it didn't change the results. I thought about the necessity of "this" at all and just removed them all entirely, but that produced the same results as well. I tried creating the new GameObject and I assigned the light to that variable through the editor. That also didn't help.
That made me realize that the lights that were flashing all had the default name of "Point light" in the editor. I altered them hoping that the engine was just "confusing" one with another, but still, no change. Thank you though. ~ True$$anonymous$$am