• 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
-2
Question by Blobfysh · Jan 06, 2014 at 02:22 AM · java

How to make toggable flashlight flicker only when on

Ok I've looked all over for a code to do this and I've been experimenting but nothing will work. So this is the script for the toggable flashlight: var flashlight : Component; var FlashLIghtOn : AudioClip; var FlashLIghtOff : AudioClip;

     function Start () {
      
     flashlight = GetComponent("Light");
      
     }
      
     function Update () {
      
     if (Input.GetMouseButtonDown(0)) {
     if (flashlight.enabled) {
     flashlight.enabled = false;
     audio.PlayOneShot(FlashLIghtOn);
      
     }
     else {
     flashlight.enabled = true;
     audio.PlayOneShot(FlashLIghtOff);
      
      
     }
     }
     }
 

and I want it to flicker only when its on. Any help? Heres my flickering light script if it helps: 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, 3.5);
 
 yield WaitForSeconds (flickerSpeed);
 
 }

Sorry I suck at code right now... Still learning

Comment
Add comment · Show 1
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 sdgd · Jan 06, 2014 at 02:39 AM 0
Share

Thumbs down because code isn't tagged correctly and so looks confusing, ...

how to properly tag your code:

use 101010 button in the edit mode and paste your code inside.

you can even watch

http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

to see how to properly as a Question

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by craigaw · Jan 06, 2014 at 06:26 AM

It seems like the randomizer variable will never return 0. The range is 0.1 to 3.5, so it will never go below zero. Even if you fix that, it is extremely unlikely that it will become exactly zero randomly.

Try something like this -

 public var flashlight : GameObject;
 private var notFlickering : boolean = true;
 
 function Update () {
     if (notFlickering)
         StartCoroutine(Flicker());
 }
 
 function Flicker() {
     notFlickering = false;
     var light : Light = flashlight.GetComponent("Light");
 
     var flickerChance : float = 0.8f;
     var flicker : boolean = false;
     if (Random.value < 0.8)
         flicker = true;
 
     var randomTime : float = 0;
     if (flicker){
         light.enabled = false;
         randomTime = Random.Range (0f, 0.1f);
     } else {
         light.enabled = true; 
         randomTime = Random.Range (0.5f, 3.5f);
     }
 
     yield WaitForSeconds(randomTime);
 
     notFlickering = true;
 }
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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Tags Help scripting 1 Answer

Data keeps "hanging". TCP client/server 2 Answers

please help me with my script 2 Answers

level to level 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