• 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 /
avatar image
0
Question by Aristotless · Sep 10, 2012 at 11:09 AM · soundlightflicker

Add sound to light effect

Hey, I'm new to Unity so please go easy on me. Anyways, I am using this script for a light effect (Flickers lights):

 var flickerSpeed : float = 0.07;
 
 private var randomizer : int = 0;
 
 while (true) {
 
     if (randomizer == 0) {
 
         light.enabled = false;
 
     }
 
     else light.enabled = true;
 
     randomizer = Random.Range (0, 1.1);
 
     yield WaitForSeconds (flickerSpeed);
 
 }

I made my own custom sound of flickering lights (its in .wav by the way if thats needed), and its very short so it has time to be played more then once and quick. So is there any way I can make the sound go off whenever the light flickers the same as the script? Thanks in return.

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
0
Best Answer

Answer by fafase · Sep 10, 2012 at 11:15 AM

 var flickerSpeed : float = 0.07;
 var sound:AudioClip;
 private var randomizer : int = 0;
     
 function Update() {
       Flicka();
 }
 
 function Flicka(){
     if(!audio.isPlaying){
         if (randomizer == 0) {
            light.enabled = !light.enabled;
            audio.Play();
        }
     randomizer = Random.Range (0, 11);
     }
 }

I change a little bit, maybe you will get back to original but keep in mind that a while(true) with no break command will simply never stop running keeping the rest of the game on hold. Drag and drop your audio to the sound slot in the Inspector.

I am assuming it works but I cannot try it now...

Edit: I removed some parts so that now every frame your light has 10% chance of switching on/off. That also means it can flick real fast or it can stay on real long. Change the random value to make the chnage happen more often. (0,2) = 50% (0,5) = 20% adnd so on. Note that the extreme value is excluded when using integer (0,2) will only returns 0 or 1.

See if that suits your effect.

Second edit: now the switching cannot happen if the sound is playing.

Comment
Add comment · Show 10 · 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 Aristotless · Sep 10, 2012 at 11:48 AM 0
Share

I'm getting an error: $$anonymous$$issingComponentException: There is no 'AudioSource' attached to the "Point light" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "Point light". Or your script needs to check if the component is attached before using it. UnityEngine.AudioSource.Play () (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/BaseClass.cs:478) Light with sound+$Flicka$31+$.$$anonymous$$oveNext () (at Assets/Light with sound.js:17) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine_Auto(IEnumerator) Light with sound:Update() (at Assets/Light with sound.js:10)

avatar image fafase · Sep 10, 2012 at 11:50 AM 0
Share

Yep, you got it all in your error message. You need to add an Audio source. Component->audio->Audio Source

avatar image Aristotless · Sep 10, 2012 at 12:20 PM 0
Share

Wow! That works thanks! But is there a way I can slow it down? Like give it a pause? As soon as it ends, it starts again like right away! How can i give it like a 0.5s pause?

avatar image fafase · Sep 10, 2012 at 12:57 PM 0
Share

In the Audio Source inspector, tick off loop. I assume yuo mean the sound is looping. I edit the answer a little.

avatar image Aristotless · Sep 10, 2012 at 01:02 PM 0
Share

Its already ticked off, and I want it to loop. Just not so fast. I want to give it like an 0.5 seconds pause before it loops again.

Show more comments
avatar image
0

Answer by lodendsg · Sep 10, 2012 at 11:16 AM

In the game object that I assume holds your light and all you can add the sound as well and then you can use audio.play(); i.e. (GameObject)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Why are objects sometime flickering? 2 Answers

Synch pulsing light to heartbeat 2 Answers

light flare flickering from particles 0 Answers

Light/Shadow Z-Fighting Styled Flickering while Camera is Moving Around 2 Answers

Light in Particles with SoftShadow flick scene 2 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