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
1
Question by jatzack · Feb 15, 2010 at 01:57 AM · lightingblinking

Blinking Lights

I want to make my lights blink. I have a game object called Main Lights. I was thinking that a possible way to make the effect would be to destroy and then instantiate Main Lights on update, but I'm not really great with code, and I feel like there must be an easier or simpler way to create the effect. Any suggestions would be helpful. Thanks!

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

4 Replies

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

Answer by Eric5h5 · Feb 15, 2010 at 02:20 AM

Indeed, repeated destroying and instantiating is not a great idea since it uses resources unnecessarily.

var timer = 1.0;

function Start () { while (true) { yield WaitForSeconds(timer); light.enabled = !light.enabled; } }

Comment
Add comment · Show 2 · 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 Uzquiano · Mar 16, 2011 at 10:45 PM 0
Share

Hi Eric,

Isn't it any problem (resources) putting a infinite loop in the start() function?

avatar image Eric5h5 · Mar 16, 2011 at 11:37 PM 0
Share

@Uzquiano: No, there's nothing inherently wrong with an infinite loop.

avatar image
2
Best Answer

Answer by Ashkan_gc · Feb 15, 2010 at 05:17 AM

if you destroy and instantiate a gameObject, you destroy allot of resources. you can change the range property of a light or enable/disable it in shor intervals to make the effect. you should not do it in Update because it will become framerate dependent. you should use a coroutine.

vat waitingTime=0.05; //20 times a second
var normalRange=3; //normal range of light
var flRange=0.5; //flickering range
function Start()
{
while (true)
{
if (light.range == normalRange) light.range = flRange; else light.range = normalRange;
yield WaitForSeconds(waitingTime);
}
}

you can turn on or off enabled property too. i'll show that in C#

public float waitingTime=0.05;
IEnumerator Start ()
{
while (true)
{
light.enabled = !(light.enabled); //toggle on/off the enabled property
yield return new WaitForSeconds(waitingTime);
}
}

i did not test any of these values and you should edit them to get best timer values.

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 jatzack · Feb 15, 2010 at 07:34 AM 0
Share

Thanks for the extra information

avatar image
1

Answer by Nomibuilder · Feb 12, 2015 at 12:58 PM

You may use this script :-)

 using UnityEngine;
     using System.Collections;
     
     public class PoliceLight : MonoBehaviour {
     
         public Light RedLight;
         public Light BlueLight;
         public int Number = 1;
         // Use this for initialization
         void Start () 
         {
             Number = 1;
             BlueLight.light.intensity = 0;
             RedLight.light.intensity = 0;
         }
         
         // Update is called once per frame
         void Update () 
         {
             if (Number == 1)
             {
                 BlueLight.light.intensity = 0;
                 RedLight.light.intensity = 1.5f;
                 StartCoroutine(waitforred());
             }
             if (Number == 2)
             {
                 RedLight.light.intensity = 0;
                 BlueLight.light.intensity = 1.5f;
                 StartCoroutine(waitforblue());
             }
         }
         IEnumerator waitforred()
         {
             yield return new WaitForSeconds (0.2f);
             Number = 2;
         }
         IEnumerator waitforblue()
         {
             yield return new WaitForSeconds (0.2f);
             Number = 1;
         }
     }
     

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 bradjensen68 · Feb 16, 2010 at 06:41 PM

You can also just have two textures - one texture lighter than the other and then swap textures with a timer. This works well for things such as blinking warning lights that do not need to cast light.

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

2 People are following this question.

avatar image avatar image

Related Questions

Emissive material not working despite setting objects to "Static" 10 Answers

Unable to bake all lights in the scene 1 Answer

characters in scene totally dark 3 Answers

orthographic camera and shadow 1 Answer

Static Objects outside the range of Point lights are baked 0 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