Flashlight On and Off Unity C# Script



This simple script can be used for switching on and off a flashlight in Unity. Add the spotlight on your flashlight to the reference slot in the script.

Here is the script:

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

public class FlashlightOnOff : MonoBehaviour
{
	
	public GameObject Flashlight;
	bool flashlightson;
	
    void Start()
    {
        flashlightson = false;
    }
	
	public void TurnOn(){
		Flashlight.SetActive(true);
		flashlightson = true;
	}
	
	public void TurnOff(){
		Flashlight.SetActive(false);
		flashlightson = false;
	}
	
	public void ToggleFlashlight(){
		if(flashlightson)
			TurnOff();
		else
			TurnOn();
	}
}

Watch the video to see how to use this script:

You can purchase the complete files as Unity Package here: https://creativeshop.ciihuy.com/product/on-off-switchable-flashlight-unity-package/

loading...

Leave a Reply

Your email address will not be published. Required fields are marked *