- Home /
Question by
EagleStr1ke · Sep 06, 2012 at 07:38 AM ·
audiosoundflashlight
Flashlight sound script won't work
Ok basically I have a script that toggles a flashlight on the player. The flashlight works fine, however no sound plays. It seemingly should and I'm out of ideas. Here's the script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class FlashlightToggle : MonoBehaviour {
private bool FlashlightOn = false;
public AudioClip FlashlightToggleSound;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("FlashlightToggle") && FlashlightOn == false){
FlashlightOn = true;
light.intensity=2;
audio.PlayOneShot(FlashlightToggleSound);
}
else if(Input.GetButtonDown("FlashlightToggle") && FlashlightOn == true){
FlashlightOn = false;
light.intensity=0;
audio.PlayOneShot(FlashlightToggleSound);
}
}
}
Comment
Best Answer
Answer by fafase · Sep 06, 2012 at 07:57 AM
I reckon you dragged the file into the slot so yes that should work. Do you know you can greatly simplified all that with:
I would consider your light is a child of the player so:
AudioClip sound;
Light spot;
void Start(){
spot = GetComponentInChildren<Light>();
spot.enabled = false;
audio.loop = true;
audio.playOnAwake=false;
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space)){
audio.PlayOneShot(sound);
spot.enabled = !spot.enabled;
}
}
This is tested in Js and working.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080707im_/https://answers.unity.com/themes/thub/images/avi.jpg)