flashlight script trouble

I have made a flashlight script following mikay on youtube but when I press F nothing happens and the light is on when it should be off at the start what is wrong heres the script

     using UnityEngine;
using System.Collections;

public class Flashlight : MonoBehaviour 
{
public Light FlashLightObject;
public float Batterypower = 1000.0f;

	
	void Start() 
	{
		FlashLightObject.enabled = false;
	}

	void Update () 
	{
	//on/off
	
	if(Input.GetKeyDown(KeyCode.F))
		{
			if(FlashLightObject.enabled == false){
			
				FlashLightObject.enabled = true;
			}
			else
			{
			FlashLightObject.enabled = false;
			}

				
	//power management 
	
			if (FlashLightObject.enabled == true)
			
			{
			Batterypower -= 1.0f;
			}
	// no power 	
			if (Batterypower <= 0)
			{
				FlashLightObject.enabled = false;
				Batterypower = 0;
		}	
	}
}

i have assigned the light in unity but as i said its on when is should be off and pressing F does nothing

ps. i mannaged to fix it but now i missed a = on if(FlashLightObject.enabled == false){

  • For the first problem, change void start () to void Start()

  • For the second, change

    if(Flashlightobject.enabled == false)
    {
    Flashlightobject.enabled = true;
    }
    else
    {
    Flashlightobject.enabled = false;
    }

to

Flashlightobject.enabled = !Flashlightobject.enabled;

Have you tried disabling the entire object instead of just the component? something like

Flashlightobject.gameObject.SetActive(false).

To keep it in one line you could write:

Flashlightobject.gameObject.SetActive(!Flashlightobject.gameObject.activeSelf)

Depending on your hierachy this might disable the entire flashlight though- But at least you’ll know if you have the right object :slight_smile: