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

.enabled does not work like it should be

Discussion in 'Scripting' started by Razora, Oct 26, 2016.

  1. Razora

    Razora

    Joined:
    Oct 26, 2016
    Posts:
    4
    Hello,

    I'm scripting a toggleable flashlight. I thought it will not be a big deal, but I have a serious problem which I can't fix myself. The flashlight is toggling from ON to OFF, but ingame there is a flashlight icon which should be displayed while the flashlight is toggled on and disapear while its turned off. That unfortunately doesn't work.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public Image m_FlashLightImage;
    7.     public Light m_Light;
    8.  
    9.     void Start ()
    10.     {
    11.         m_Light.intensity = 0;
    12.         m_FlashLightImage.enabled = false;
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         ToggleFlashlight();
    18.     }
    19.  
    20.     void ToggleFlashlight()
    21.     {
    22.         if(Input.GetKeyDown(KeyCode.F) && m_Light.intensity == 0)
    23.         {
    24.             m_Light.intensity = 5.5f;
    25.             m_FlashLightImage.enabled = true;
    26.  
    27.         }
    28.         else if (Input.GetKeyDown(KeyCode.F) && m_Light.intensity == 5.5f)
    29.         {
    30.             m_Light.intensity = 0;
    31.             m_FlashLightImage.enabled = false;
    32.         }
    33.     }
    Maybe someone can help me :)

    Another problem I have is, you see I have m_FlashLightImage.enabled set to false in void Start(). So it SHOULD not be drawn at the beginning. But it does. It disapears when I have set the flashlight to OFF and exit the game. Same thing with my HealthBar.. It updates AFTER I exit the playmode, even though its in my void Start().
     
  2. KingArri

    KingArri

    Joined:
    Jul 2, 2015
    Posts:
    3
    try using Awake() instead of Start(). awake happens before start so maybe that will help with the updating issue
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I created a 3d Project. I added a default Unity sphere and default spotLight pointed on to the sphere.. Then I created a UI Image (which created the Default canvas in ScreenSpace-overlay mode). I just gave the image the default checkmark and put it in the upper left corner of the screen

    I then copied your code as posted above and attached my image and spotlight too it. When I hit play both the spotlight and the image are off, and when I hit F they toggle on and off as expected.

    So I think the code above works 100% as intended and the bug must be in some other code you have. How are you setting up your image, and does anything else interact with the spotlight and the image?

    Also you shouldn't move the code to Awake. Since your modifying values of other objects, your not 100% sure they have been instantiated yet in Awake. You were correct to place the code you have in the Start method.
     
    Razora likes this.
  4. Razora

    Razora

    Joined:
    Oct 26, 2016
    Posts:
    4
    Okay I see what I did wrong, but I have another question now.
    The problem which I had was that I took the flashlight UI Image from the Projekt Folders instead of the Hierarchy. I thought it will be the same when I make a prefab of the UI Image and use the prefab instead. I saw it because the prefab in my Projekt Folder was toggling ON and OFF. Now to my next question.

    I dragged the Hierarchy UI Image of my flashlight into the Script instead of the prefab.
    Now the text where I dragged it is Bold. What does that mean? Even when I hit apply it doesn't change to normal and keeps bold. It means that this value isn't saved, right? But apply isn't working for me :(
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It means the value you've assigned to that field is different than the "default" value in the prefab. It's perfectly normal and expected when a prefab instance references other scene objects.
     
    Razora likes this.
  6. Razora

    Razora

    Joined:
    Oct 26, 2016
    Posts:
    4
    Thank you very much!