• 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 Anchientstar · Oct 21, 2013 at 11:48 PM · javascriptrandomlights

How to turn a light on and off randomly?

Hey, So i'm trying to create a sheet lighting type of feel using a massive directional light. How do I program it so it randomly turns on and off rapidly 3 or 4 times? I'm using JavaScript.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mattmanj17 · Oct 22, 2013 at 12:19 AM

http://docs.unity3d.com/Documentation/Manual/RandomNumbers.html

Try reading through the unity manual. it has lots of great answers to simple questions like this. unity answers is a good resource for technical questions that cannot be easily answered by looking at the docs or the forums, but most of the time either of those options is a beter bet.

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
avatar image
0

Answer by Cherno · Oct 22, 2013 at 12:28 AM

 var myLight : GameObject;
 
 var randomThreshold : float;//don't touch
 var minThreshold : float = 2.0;//minimum time between flashes
 var maxThreshold : float = 6.0;//maximum time between flashes
 
 var timer : float;//don't touch
 var isFlashing : boolean;//don't touch
 
 var randomFlashes : int;//don't touch
 var minFlashes : int = 3;//minimum number of flashes
 var maxFlashes : int = 5;//maximum number of flashes (maximum is exclusive!)
 
 var timeBetweenFlashes : float = 0.1;//
 
 function Start()
 {
     randomThreshold = Random.Range(minThreshold, maxThreshold);
     randomFlashes = Random.Range(minFlashes, maxFlashes);
 }
 
 function Update()
 {
     if(isFlashing == false)
     {
         timer += Time.deltaTime;
     }
     
     if(timer > randomThreshold)
     {
         timer = 0.0;
         FlashingLights();
     }
 }
 
 function FlashingLights()
 {
     isFlashing = true;
     
     for(var x = 0; x < randomFlashes; x ++)
     {
         myLight.GetComponent(Light).enabled = false;
         yield WaitForSeconds(timeBetweenFlashes);
         myLight.GetComponent(Light).enabled = true;
         yield WaitForSeconds(timeBetweenFlashes);
     }
     
     //Reset variables for the next flashing
     randomThreshold = Random.Range(minThreshold, maxThreshold);
     randomFlashes = Random.Range(minFlashes, maxFlashes);
     
     
     isFlashing = false;
 }
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
avatar image
0

Answer by aldonaletto · Oct 22, 2013 at 12:32 AM

You can turn it on and off at random intervals with a coroutine, like this (attach it to the light game object):

 private var flickering = false; // is true while flickering

 function Flicker(howManyTimes: int){
   if (flickering) return; // abort further calls while running
   flickering = true;
   while (howManyTimes > 0){
     light.enabled = false; // turn off for a random time
     yield WaitForSeconds(Random.Range(0.1, 0.9);
     light.enabled = true; // turn on for another random time
     yield WaitForSeconds(Random.Range(0.1, 0.9);
     howManyTimes--; // repeat howManyTimes
   }
   light.enabled = false; // turn it off and return
   flickering = false; // Flicker finished
 }

Call Flicker(4) to blink four times, for instance. It's better to check whether it's already running before calling Flicker in order to avoid unnecessary memory allocation:

 if (!flickering) Flicker(4);
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

17 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

Related Questions

Random position without overlap. HELP! 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Help with spawning an object in a random location 1 Answer

How to create random movement in 2D 2 Answers

Random Array of sounds for Random Game Object 1 Answer


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