Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TrueKam · May 22, 2014 at 12:26 PM · c#lighting

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zodiarc · May 22, 2014 at 12:46 PM 0
Share

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.

avatar image TrueKam · May 22, 2014 at 12:52 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TrueKam · May 22, 2014 at 12:42 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Distribute terrain in zones 3 Answers

Making a light turn on and off with the same button 1 Answer

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges