Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Light.enabled = false not working?

Discussion in 'Scripting' started by ghostpixelstudios, Dec 27, 2020.

  1. ghostpixelstudios

    ghostpixelstudios

    Joined:
    Dec 27, 2020
    Posts:
    3
    Hey there! I'm currently trying to make a low-poly, low-resolution, survival horror game with some friends of mine and for some reason whenever I try to disable and enable the flashlight the following code doesn't work!

    flashlight.enabled = false;


    The flashlight variable represents the light in the game world and whenever I try to disable or enable the component it simply doesn't happen for whatever reason.

    I've tried numerous times to find a working solution, however; debugging and bug-fixing hasn't really worked out so I finally decided to take to the forms now that my head is clean out of ideas.

    I don't really have anything else to go off of in terms of code unless I give out the entirety of the file.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What do you mean debugging and bug fixed hasn't worked?, what did you do?
    did you check that the code is even running?
    did you forget to put the script on the object?
    forgot to link a reference? errors in the console?

    more information needed, how are we suppose to help?, what I posted above is debug 101.
     
  3. ghostpixelstudios

    ghostpixelstudios

    Joined:
    Dec 27, 2020
    Posts:
    3
    I've tried using DebugLog and rewriting the script multiple times but for some reason the enable variable on the light just will not turn off even if I put it in the update method. For instance;

    Code (CSharp):
    1. private void Update()
    2. {
    3.     flashlight.enabled = false;
    4. }
    This bit of code does not work either ^

    if it was a "not being called" issue, then it wouldn't be doing something like this right?

    I've re-checked all references for the script and the script is on the object and everything should be referencing properly.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    What happens if you put this right before you attempt to manipulate the .enabled property of your flashlight:

    Code (csharp):
    1. Debug.Log( flashlight.name);
    That will show you the name of the gameObject containing the light.

    Being realistic, we KNOW that the .enabled field didn't magically stop working. We know this. Therefore you have a bug that you have to track down. To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    Remember the six steps of debugging:

    1. That can’t happen.
    2. That doesn’t happen on my machine.
    3. That shouldn’t happen.
    4. Why does that happen?
    5. Oh, I see.
    6. How did that ever work?
     
    SparrowGS likes this.
  5. ghostpixelstudios

    ghostpixelstudios

    Joined:
    Dec 27, 2020
    Posts:
    3
    I've decided to try rewriting the entire script and instead of using the flashlight.enabled = false bit I just decided to make the gameobject for the flashlight inactive and active depending on the states and it's worked fine so far. Thanks for most of the help by the way guys! It's come in handy for trying to debug the new flashlight script!
     
    Kurt-Dekker likes this.