0
\$\begingroup\$

I'm fairly new to Unity, and I have a script I'm working with that plays two different flashlight sounds (one for the off and one for the on position).

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;

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 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.

When I start the game, the flashlight is on by default. Any ideas how to modify the script so that my character starts with the flashlight set to off?

Components added to FPSController, NOT FirstPersonCharacter

Components added to FPSController, NOT FirstPersonCharacter

Thanks in advance for any guidance!

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

You could disable the Light component in the gameobject / prefab, or just make sure the flashlight is disabled in Start:

void Start ()
{
    isActive = false;
    flashLight.enabled = false;
}

By the way: I find it a bit weird that you have the line isActive=true in your Start-method, and then wonder why it starts in the enabled state. I mean, it wouldn't work to just set this one to false, because it only affects the state of this controller, not that of the light. But it makes me think that you perhaps copied that code from somewhere. It's generally not a good idea to just copy code into your project when you don't understand how it works and what it does. That might cause lots of problems down the line.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the guidance! Your suggestion worked. And you're right, I did copy the code from a tutorial, thinking it was just kind of a simple/generic script for a light effect. And still definitely in the early stages of learning C# and getting a more precise understanding of every function. I can see what you're saying re the line affecting the controller as opposed to the light. Again, thanks very much for the fix! \$\endgroup\$
    – Darko
    Commented Dec 17, 2022 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .