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 /
avatar image
0
Question by verschurengiovanni · Sep 03, 2019 at 09:27 PM · unity 5script.

Strobe light when a key is pressed

Hi!

I'm making a funfair game, and I need a strobe or flicker light script.

I want a light to strobe/flicker when a key is being pressed. I made a code that flickers the light automatically, but I can't made one yet when a key is being pressed. I hope somebody can help me with this.

 using UnityEngine;
 using System.Collections;
 
 
 public class Flitser : MonoBehaviour
 {
 
 
     Light testLight;
     public float minWaitTime;
     public float maxWaitTime;
 
     void Start()
     {
         testLight = GetComponent<Light>();
         StartCoroutine(Flashing());
     }
 
     IEnumerator Flashing()
     {
         while (true)
         {
             yield return new WaitForSeconds(Random.Range(minWaitTime, maxWaitTime));
             testLight.enabled = !testLight.enabled;
 
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Sep 04, 2019 at 06:43 AM

If you want the light to flicker when a key is held down.

 using UnityEngine;
 using System.Collections;
 
 public class Flitser : MonoBehaviour
 {
     Light testLight;
     public float minWaitTime;
     public float maxWaitTime;
     public KeyCode triggerKey = KeyCode.Space;
     private float timer = 0;
 
     void Start()
     {
         testLight = GetComponent<Light>();
         timer = Random.Range(minWaitTime, maxWaitTime);
     }
 
     private void Update()
     {
         if( Input.GetKey(triggerKey) )
         {
             timer -= Time.deltaTime;
             if ( timer < Time.deltaTime )
             {
                 timer = Random.Range(minWaitTime, maxWaitTime);
                 testLight.enabled = !testLight.enabled;
             }
         }
         else
         {
             timer = 0;
 
             // Following lines optional
             // Just in case you always want the light to be enabled
             // if the key is not held anymore
             if(Input.GetKeyDown(triggerKey))
             {
                 testLight.enabled = true;
             }
         }
     }
 }


If you want the light to flicker after a key is pressed down and until another key is pressed down (switch on/off with 2 different buttons)

 using UnityEngine;
 using System.Collections;
 
 public class Flitser : MonoBehaviour
 {
     Light testLight;
     public float minWaitTime;
     public float maxWaitTime;
     public KeyCode enableKey = KeyCode.O;
     public KeyCode disableKey = KeyCode.L;
     private float timer = 0;
     private bool flickering;
 
     void Start()
     {
         testLight = GetComponent<Light>();
         timer = Random.Range(minWaitTime, maxWaitTime);
     }
 
     private void Update()
     {
         if( flickering )
         {
             timer -= Time.deltaTime;
             if ( timer < Time.deltaTime )
             {
                 timer = Random.Range(minWaitTime, maxWaitTime);
                 testLight.enabled = !testLight.enabled;
             }
             
             if( Input.GetKeyDown(disableKey) )
             {
                 timer = 0;
                 flickering = false;
                 
                 // Following line optional
                 // Just in case you always want the light to be enabled
                 testLight.enabled = true;
             }
         }
         else if( Input.GetKeyDown(enableKey) )
         {
             flickering = true ;
         }
     }
 }
Comment
Add comment · Show 4 · 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 verschurengiovanni · Sep 04, 2019 at 11:25 AM 0
Share

Amazing! This works! Also, I have a few spot lights in different colours, If a user want to enable/disable them with the same or different key.

How can I do that?

So for example, the $$anonymous$$ey "O" enables the lights and the key "L" disables the lights

avatar image Hellium verschurengiovanni · Sep 04, 2019 at 11:47 AM 0
Share

I didn't understand everything, but I've updated the answer with a new code to enable the flickering with a key, and disable it with another key.

avatar image verschurengiovanni Hellium · Sep 04, 2019 at 11:52 AM 0
Share

I don't mean a flicker with a different key, I mean just a static light, that goes on/off with the keys "O" and "L" It's hard to explain, English is my second language.

Show more comments

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

203 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

Related Questions

Help adjusting my code 2 Answers

How to load a different scene at start of game? 2 Answers

Problem with "Using Unity Standard Assets" in script C# 1 Answer

Assign rotation of GameObject by using a Vector3 as variable 1 Answer

Point Counter Works Only Once! 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