Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

i want my flashlight to do sound when turned on and off

Discussion in 'Scripting' started by mowmizyd, Oct 12, 2020.

  1. mowmizyd

    mowmizyd

    Joined:
    Oct 11, 2020
    Posts:
    8
    Error: The name "audioSource" does not exist in current context
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Flashlight : MonoBehaviour
    6. {
    7.     public Light flashLight;
    8.     public AudioSource AudioSource;
    9.  
    10.     public AudioClip soundFlashlightOn;
    11.     public AudioClip soundFlashlightOff;
    12.  
    13.     private bool isActive;
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         isActive = true;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.F))
    25.         {
    26.             if (isActive == false)
    27.             {
    28.                 flashLight.enabled = true;
    29.                 isActive = true;
    30.  
    31.                 audioSource.PlayOneShot(soundFlashlightOn);
    32.                  
    33.             }
    34.             else if (isActive == true)
    35.             {
    36.                 flashLight.enabled = false;
    37.                 isActive = false;
    38.  
    39.                 audioSource.PlayOneShot(soundFlashlightOff);
    40.             }
    41.         }
    42.     }
    43. }
    44.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Capitalization must be 100% correct, just like spelling and punctuation.
     
    Joe-Censored and mowmizyd like this.
  3. mowmizyd

    mowmizyd

    Joined:
    Oct 11, 2020
    Posts:
    8
    Thanks ^-^