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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Unity flashlight issue

Discussion in 'Scripting' started by DomDominator, Feb 6, 2023.

  1. DomDominator

    DomDominator

    Joined:
    Jan 17, 2023
    Posts:
    6
    Hello everyone I have a problem with this script. When it is OFF it does not turn ON

    I physically enabled spotlight in the scene to see whether is will turn off and it does but it wont go back on, so the problem is with turning it on, it seems turn off works. Any help would be appreciated

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Flashlightt : MonoBehaviour
    {

    [SerializeField] GameObject Flashlightlight;
    private bool FlashlighttActive = false;

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKey(KeyCode.F))
    {
    if (FlashlighttActive == false)
    {
    Flashlightlight.gameObject.SetActive(true);
    FlashlighttActive = true;
    }
    if (FlashlighttActive == true)
    {
    Flashlightlight.gameObject.SetActive(false);
    FlashlighttActive = false;
    }
    }
    }
    }
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Please use Code-tags for your code.

    One of the issues I can already see in this code is that you're using Input.GetKey(). That will return true for every frame that the key is HELD. Thus, you're turning it off & on 60 times per second (if running at 60fps).

    Use GetKeyDown() instead. That will only return true in the frame that the key was first pushed.

    Edit: Another issue is that you're setting FlashlighttActive to true, then checking
    if (FlashlighttActive == true)
    .. This WILL be true, since you just set it to true 2 lines above.
     
  3. DomDominator

    DomDominator

    Joined:
    Jan 17, 2023
    Posts:
    6
    I did try other ways to make it work, it is this now but still doesn't work

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Flashlightt : MonoBehaviour
    {

    [SerializeField] GameObject Flashlightlight;
    private bool FlashlighttActive = false;

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.F))
    {
    if (FlashlighttActive == false)
    {
    Flashlightlight.gameObject.SetActive(true);
    FlashlighttActive = true;
    }
    else
    {
    Flashlightlight.gameObject.SetActive(false);
    FlashlighttActive = false;
    }
    }
    }
    }
     
  4. DomDominator

    DomDominator

    Joined:
    Jan 17, 2023
    Posts:
    6
    any idea what is wrong?
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Again: Please use Code-Tags, because this is hard to read.

    Using code tags properly - Unity Forum

    The code as-is SHOULD be toggling your GameObject on every press of F.
    Have you tried printing/logging what happens?
     
  6. DomDominator

    DomDominator

    Joined:
    Jan 17, 2023
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Flashlightt : MonoBehaviour
    6. {
    7.  
    8. [SerializeField] GameObject Flashlightlight;
    9. private bool FlashlighttActive = false;
    10.  
    11. // Update is called once per frame
    12. void Update()
    13. {
    14. if (Input.GetKeyDown(KeyCode.F))
    15. {
    16. if (FlashlighttActive == false)
    17. {
    18. Flashlightlight.gameObject.SetActive(true);
    19. FlashlighttActive = true;
    20. }
    21. else
    22. {
    23. Flashlightlight.gameObject.SetActive(false);
    24. FlashlighttActive = false;
    25. }
    26. }
    27. }
    28. }
    29.  
    yeah my code works, i close the thread - thanks for telling me about the code-tags will use it in the future.
    The problem was the code was attached to the light itself instead of to the camera so when it turned off the object the script was no longer executed hence the issue - yes I know its silly and it always happen to me it just takes me 5h to figure out something simple
     
    SF_FrankvHoof likes this.