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 /
  • Help Room /
avatar image
0
Question by Vid-Maddness · Jan 31, 2016 at 07:25 PM · javascriptgameobjectcolorspotlight

Change flashlight color when object is picked up,

I'm trying to make it so when you pick up a gameobject the spotlight color will change.

 var lt: Light;
 var MainCam : GameObject;
 
 function Start () {
     MainCam = GetComponent.<Light>();
 }
 
 
 function Update () {
 if (Input.GetKeyUp(KeyCode.E))
     lt.color -= Color.blue/ 2.0 * Time.deltaTime;
 }


The code will not work, how can i fix it?,

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 Namey5 · Mar 28, 2016 at 12:44 AM 1
Share

You are setting your '$$anonymous$$ainCamera' (GameObject) as a light. Change this to 'lt' ins$$anonymous$$d.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by bayerly · Mar 28, 2016 at 11:46 PM

Agreed with Namey5 (who puts lots of possible answers as Replies for some reason!):

MainCam is being declared as a GameObject, yet you're setting it as a Light component...Basically, I think I may see what you're getting at.

Rather than 'MainCam' you should probably say "var flashlight : GameObject;", then get its Light component (not sure if this is the most efficient way, but it's fairly readable). You assign the "flashlight" var in the Inspector or assign it in the Start() function somehow.

 #pragma strict
 var flashlight : GameObject;
 
 function Update () {
 
     if (Input.GetKeyDown("space")){
 
         flashlight.GetComponent.<Light>().color = Color.blue;
 
     }
 
 }

It also looks like maybe you're trying to have it fade out or something afterward? Multiplying by time could produce strange results, and I'm not sure you can use a '-=' with regards to colors...or division. But I may be wrong.

If you're trying to have something that transitions between colors depending on what's going on, you might want to set up something with, say, Color.Lerp. A basic example:

 #pragma strict
 var flashlight : GameObject;
 var lerpedcolor : Color;
 
 var currentcolor : Color;
 var changedcolor : Color;
 
 var lerptime : float = 1.0;
 
 
 function Update () {
 
     flashlight.GetComponent.<Light>().color = currentcolor;
 
     currentcolor = Color.Lerp(currentcolor, changedcolor, lerptime * Time.deltaTime);
 
 
     if (Input.GetKeyDown("space")){
 
         changedcolor = Color.blue;
 
     }
 
     if (Input.GetKeyDown("f")){
 
         changedcolor = Color.white;
 
     }
 
 }


So Lerp and Slerp and other functions that transition between two values can be confusing...they took me a long time to figure out, and in this case Lerp may not be the best choice. But what's going on in this script is this - the flashlight's color is continuously listening to what the Lerp is doing. Then you change the values that the Lerp is using, and it moves toward "changedcolor".

Not sure if that's what you were looking for, but hopefully it'll help you out! Let me know if you have any questions.

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 Namey5 · Mar 29, 2016 at 04:03 AM 2
Share

Yeah, I was replying because a lot of the questions weren't really worth answering, i.e. it was too simple and they may have been looking for some broader approach. I just noticed that in this particular code, the light has already been declared at the top, it just isn't assigned and ins$$anonymous$$d the main camera is.

avatar image bayerly Namey5 · Mar 29, 2016 at 07:50 PM 0
Share

Totally makes sense - good call!

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

53 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

Related Questions

Make a relation between color and distance between objects' x coordinates 0 Answers

Copying or duplicating a gameObject from one scene to another 1 Answer

How can I get my camera to follow a clone after a certain trigger occurs? 2 Answers

how to enable a java script inside a gameobject on press of the space key? 1 Answer

can you control 2 gameobject color values without having to reference both ? 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