Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by BeanSlice · Apr 24, 2017 at 05:43 PM · c#errorflashlightsupport

Flashlight script help!

I'm new to C# and am aware of many different solutions to my problem, but basically, I was wondering if there is a way to use a flashlight sound for on and a flashlight sound for off when using light.enabled = !light.enabled. If there is a solution that would be great, but I cant find one, so I resorted to writing this alternative script. But for some reason, the play in "audio.Play" is highlighted in red and says it does not exist in this context. I've had a look at Unity's documentation and I cannot spot any differences. Can anybody help me out?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class flashlight2 : MonoBehaviour 
 {
 
     public Light light;
     public AudioClip soundOn;
     public AudioClip soundOff;
 
     void Start () 
     {
         light.enabled = false; 
     }
 
     void Update () 
     {
         if (Input.GetKeyDown (KeyCode.F)) 
         {
         if (light.enabled == true)
             light.enabled = false;
             audio.Play(soundOn);
         } 
         else 
         {
             light.enabled = true;
             audio.Play(soundOff);
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by The-Evster · Apr 24, 2017 at 06:40 PM

You need a reference to the audio source. At the moment the audio in audio.Play(); means nothing. Try this

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class flashlight2 : MonoBehaviour 
  {
  
      public Light light;
      public AudioClip soundOn;
      public AudioClip soundOff;
      public AudioSource audio;
  
      void Start () 
      {
          light.enabled = false; 
          audio = GetComponent<AudioSource>();
      }
  
      void Update () 
      {
          if (Input.GetKeyDown (KeyCode.F)) 
          {
          if (light.enabled == true)
              light.enabled = false;
              audio.Play(soundOn);
          } 
          else 
          {
              light.enabled = true;
              audio.Play(soundOff);
          }
      }
  }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BeanSlice · Apr 24, 2017 at 06:50 PM 0
Share

Thanks man! I changed the script a lot, back to what it was before. How can I incorporate two sounds into this script? (Ignore the whole text thing - I'm experimenting with UI)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; //this allows us to reference anything to do with the UI in our script.
 
 public class flashlight : $$anonymous$$onoBehaviour 
 {
 
     public Light lightSource; //allows us to add a light source to be toggled.
     public AudioClip soundFile; //allows us to add a sound to be played everytime the flashlight is toggled.
     public $$anonymous$$eyCode key; //this will allow us to choose our desired key to toggle the flashlight outside of the script.
     public Text isontext; //this is used as a reference to the text that will be updated by our script.
     private bool isOn = false; //this will make the text display the light as being off at the beginning of the game, rather than displaying nothing.
     AudioSource flashlightOn;
 
     void Awake () //awake is used to initiate game states and variables before the game starts - this is only ran once during the script's lifetime. We only need to grab the audio source once, so we used awake to do so.
     {
         flashlightOn = GetComponent<AudioSource> (); //grabs the audio source component - allows sound to be played.
     }
 
     void Start () //Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
     {
         lightSource.enabled = false; //this will start the game with the flashlight off.
         displaytext();
     }
 
     void Update () //anything within the update function will be called/updated every frame.
     {
         if (Input.Get$$anonymous$$eyDown (key)) 
         {
             lightSource.enabled = !lightSource.enabled; //the "!" operator means not, you can toggle something by setting it to the opposite value of itself, which is what we are doing here with the light source. You can toggle a light source by toggling its enabled state.
             flashlightOn.PlayOneShot (soundFile, 0.8f); //plays the attached sound file once at a volume of 0.8. the f at the end of 0.8f indicates that it is a float number - hence the decimal point.
             isOn = !isOn; //the boolean isOn will be toggled when the specified key is pressed.
             displaytext();
         }
     }
     void displaytext () //using the same line of code more than once in inefficient, therefore I made the line of code a function, so ins$$anonymous$$d of using that same line of code, I can call the function. This uses less memory.
     {
         isontext.text = "Light: " + isOn.ToString (); //the boolean variable isOn will be displayed on the screen whether it is true or false (testing text).
     }
 
 }
avatar image
0

Answer by krzysztof_janik · Apr 25, 2017 at 05:11 AM

To play audio You should use AudioSource instead of AudioClip. You also miss some curly brackets in your script for proper logic.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

349 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problems with mopub 0 Answers

Show ad after certain line of code is called 5 times 1 Answer

Hello everyone I have a problem with rounding number of my score 2 Answers

error cs0131 - The left-hand side of an assignment must be a variable, a property or an indexerplease. help! 1 Answer

GetComponentInChildren not working , why ? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges