Search Unity

Flashlight

Discussion in 'Scripting' started by BenManu, Jan 13, 2021.

  1. BenManu

    BenManu

    Joined:
    Sep 25, 2020
    Posts:
    3
    So i want to make flashlight and i downloaded some model from internet
    and i attached spot light component to this model. Now i want to turn on and off this flashlight.
    I know i can simply make visible or invisible this light in editor but my player camera can see it whatever light is on or off. I dont have any idea :(
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    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.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    When you post code, please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    BenManu likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Try creating a script with a reference to the spot light component. Attach the script to somewhere that makes sense, like to the flashlight object, the player, etc. Drag the spot light component to the reference on the instance of the new script. In the script write your logic for turning on/off the light. How you actually turn it on and off in the script is just enabling/disabling the spot light component via the reference to it. That should activate/deactivate the light.

    If you run into trouble, try posting what you have for the script, and people can help further. As already mentioned, post code using CODE tags. See Kurt's link.
     
    BenManu likes this.