Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mcfetrmatt · May 30, 2012 at 11:35 PM · lightdelayflashing

Enable scripts on a directional light

Hi there,

I have a script on a directional light to make it flash. What I want to do is to enable that script after a collision. Any help would be greatly appreciated.

Here is my code so far:

 var flashinglight: GameObject;
 
 function OnTriggerEnter(trigger: Collider){
     flashinglight.light(typeof(LightFlashing)).enabled=true; 
 
 }

I am getting this error too:

Assets/Scripts/LightingFlashingEnable.js(4,23): BCE0077: It is not possible to invoke an expression of type 'UnityEngine.Light'.

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

Answer by kolban · May 30, 2012 at 11:42 PM

A GameObject can have a light attached to it. If it does, that light can be accessed as an Object of type "Light" from the property called "light" on the GameObject. So, in your example:

 flashinglight.light

will be an object of type Light. This has a property on it called enabled which defines whether or not the light is enabled or disabled. So to enable the light, one would code:

 flashinglight.light.enabled = true;

In your code, you had coded

 flashinglight.light(....)....

which basically tried to invoke a method called "light" that would be on the GameObject. However, "light" is a Light object on the GameObject and not a function.

Comment
Add comment · Show 5 · 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 mcfetrmatt · May 30, 2012 at 11:52 PM 0
Share

So I would need to replace .light with .(name of script to enable)?

I tried to adapt the code to this but the light starts flashing from when the scene is loaded, no errors thought:

var flashinglight: GameObject;

var trigger: GameObject;

function Awake(){

 flashinglight.light.enabled = false; 

}

function OnTriggerEnter(trigger: Collider){

 flashinglight.light.enabled = true; 

}

avatar image kolban · May 30, 2012 at 11:59 PM 1
Share

What does the script look like that you have attached to the light which causes it to flash? I am guessing that there is an Update() method on it that checks what time it is and sets the light on or off accordingly. What I think you want to do is to create an exposed boolean property in your script ... call it "flashingEnabled". In your Update() method, have it check the flashingEnabled boolen. If that is OFF then your Update() should set the light off and return immediately. If it is ON then you are executing your time based flash code. Now, when a collision occurs, you will want to obtain a reference to your script and set its flashingEnabled property to true.

avatar image mcfetrmatt · May 31, 2012 at 12:08 AM 0
Share

Here is the script I have attached to the light, it is a bit of a mash of different scripts I have found.

var $$anonymous$$Time = .5;

var thresh = .5;

var enable: boolean = false; // boolean flag: enable trigger 1 when true

private var lastTime = 0;

private var myLight;

// function called by trigger2 via Send$$anonymous$$essage:

function EnableTrigger(onOff: boolean){

enable = onOff;

}

function OnTriggerEnter(trigger: Collider){

 myLight = GetComponent(Light);

}

function Update (){

 if ((Time.time - lastTime) > $$anonymous$$Time)

     if (Random.value > thresh)

         light.enabled = true;

     else

         light.enabled = false;


 lastTime = Time.time;

}

avatar image kolban · May 31, 2012 at 12:15 AM 1
Share

That is what I was roughly guessing the script would contain. I'm not sure what to offer now. In my last post, I gave the high level recipe as to what would be needed. For me to code it up wouldn't take long but then we'd have to exchange code etc etc. I'm not sure what to suggest. I'm working on my own projects and am happy to assist with short questions ... but time is precious and I can't really code up other folks scripts.

avatar image mcfetrmatt · May 31, 2012 at 12:22 AM 0
Share

Yes I completely understand! Thank you for what you have done :) I'll try to give it a go and make sense of it. It's more direction than I had before

avatar image
2

Answer by whydoidoit · May 31, 2012 at 12:46 AM

I'm presuming that the script in the question is added to the thing that collides with the light?

In which case you need to be getting your script and just turning it on and off, now presuming it might one day collide with other things I suggest it look like this:

 function OnTriggerEnter(trigger: Collider){
     var flashingLight = trigger.gameObject.GetComponent(LightFlashing);
     if(flashingLight != null)
          flashingLight.enable = true;
 
 }

I presume your script in the comments is called LightFlashing for this to work, it can be simpler:

 var minTime = .5;
 var thresh = .5;
 
 var enable: boolean = false; // boolean flag: enable trigger 1 when true

 function Start() {
      light.enabled = false;//Or true if you want it on to start with
 }

 private var lastTime = 0;
 function Update (){
 if(!enable) 
     return;
 
 if ((Time.time - lastTime) > minTime) {
 
     if (Random.value > thresh)
 
         light.enabled = true;
 
     else
 
         light.enabled = false;
 
 
    lastTime = Time.time;
    }
 }

To get this to trigger there must be colliders and one of the objects must have a rigidbody (it can be isKinematic = true)

Now once activated your flashing light is going to keep flashing.... You haven't got code to turn it off there...

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 mcfetrmatt · May 31, 2012 at 01:06 AM 0
Share

@whydoidoit This worked really well thanks!! The only thing is when I replaced my flashing code with what you wrote the light doesn't flash, it only stays on.

I haven't got code to end the flashing as (for now at least) I want it to not stop.

Thanks

avatar image Bunny83 · May 31, 2012 at 01:14 AM 0
Share

I guess you're missing brackets for the if ((Time.time - lastTime) > $$anonymous$$Time). Your lastTime variable get set every frame at the moment.

avatar image whydoidoit · May 31, 2012 at 01:14 AM 0
Share

Yeah I see why - I've edited the answer and fixed that. (The lastTime was happening every update - stopping it from every changing! I added { and } to fix it).

I've also added a Start() to disable (or enable) the light when the game runs

avatar image Bunny83 · May 31, 2012 at 01:15 AM 0
Share

:D refresh and it's fixed... well done.

avatar image mcfetrmatt · May 31, 2012 at 01:20 AM 0
Share

hmmm still doesn't flash after the collision...

I have the top code attached to the trigger object and the bottom code attached to the directional light

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make flashing lights faster? 1 Answer

Flashes of light 0 Answers

Directionnal light flashing 0 Answers

Why isn't my flashing script working? 1 Answer

Rendering sun-shafts even when light is not in view. 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