Need Help

Hi there i made a flashlight script and i want to make it better by adding a sound effect when you toggle it can you help me with this?

Here is the code
Thanks…

using UnityEngine;
using System.Collections;

public class FlashLight : MonoBehaviour {
	
	private bool FlashLightOn = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	if(Input.GetButtonDown("togglelight") FlashLightOn == false){
			FlashLightOn = true;
			light.intensity = 2;
		}
		else if(Input.GetButtonDown("togglelight") FlashLightOn == true){
			FlashLightOn = false;
			light.intensity = 0;
		}
	}
}

Inside the if-statement for turning the flashlight on/off, just play the audio by calling audio.play. This is assuming the audio source is in the same game object as the script is in. Otherwise, just pass in the audio source and call that audio source to play the audio.

Try something like below. You should really just go through some Unity coding tutorials - they will teach you how to do all that stuff.

using UnityEngine;
using System.Collections; 

public class FlashLight : MonoBehaviour {
    private bool FlashLightOn = false;
    public AudioClip soundOn;
 	public AudioClip soundOff;

    // Use this for initialization
    void Start () {
    

    }   

    // Update is called once per frame

    void Update () {

    if(Input.GetButtonDown("togglelight") FlashLightOn == false){

            FlashLightOn = true;

            if (soundOn)
            	audio.PlayOneShot(soundOn);

            light.intensity = 2;

        }

        else if(Input.GetButtonDown("togglelight") FlashLightOn == true){

            FlashLightOn = false;

            if (soundOff)
            	audio.PlayOneShot(soundOff);

            light.intensity = 0;
        }
    }
}

Thanks :slight_smile:

do you know how to do battery life and recharge. pm me ill pay for the script :slight_smile: