Hi all,
I’m fairly new to Unity, and I’m trying to attach a script to my FPS character that will play sound 1 when the flashlight is toggled on, and sound 2 when it’s toggled off.
Here’s the script I’m currently working with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlashlightClick : MonoBehaviour
{
public Light flashLight;
public AudioSource AudioSource;
public AudioClip soundFlashlightOn;
public AudioClip soundFlashlightOff;
private bool isActive;
// Use this for initialization
void Start ()
{
isActive = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
if (isActive == false)
{
flashLight.enabled = true;
isActive = true;
AudioSource.PlayOneShot(soundFlashlightOn);
}
else if (isActive == true)
{
flashLight.enabled = false;
isActive = false;
AudioSource.PlayOneShot(soundFlashlightOff);
}
}
}
}
The (ridiculous as though it may seem) problem I’m having now, is actually assigning the appropriate object to the variables I’ve created. I’ve attached this script to the FirstPersonCharacter controller, under which I also have a flashlight that I’ve created attached.
The script creates four variables: Flash Light, Audio Source, Sound FlashLight On, and Sound FlashLight Off.
On the Flash Light variable, I have the flashlight (just the spotlight game object), on Audio Source, I have the FPSController object, and on Sound FlashLight On and Sound FlashLight Off I have the two sounds for the flashlight toggles.
The problem I’m running into is, the sounds play, but the light itself doesn’t toggle on. It just stays off.
Any idea what I’m doing wrong? Any help is greatly appreciated!