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 chris83 · Nov 24, 2012 at 02:44 PM · javascriptbeginnerlightssimple

How to switch off two lights?

Hello UnityAnswers,

I am a beginner at Unity and want to switch off the headlights of a car. Therefor I created two spot lights and linked them. Actually, I don't know if this is linking or if this is childing?

two Headlight

In my JavaScript-File I have defined a variable, like this:

 var lightToToggle    :    GameObject;

Clicking on a button calls the function which performs follwing:

  function OnMouseDown(){
     if(lightToToggle.light.enabled){
          lightToToggle.light.enabled = false;
     }
     else{
          lightToToggle.light.enabled = true;    
     }
  }

Unfortunately only the (blue marked) headlight goes on and off. Do I have to access the children (if this is a parent-child-situation). And if so, how do I get the child?

Any help is appreciated!

Greetz Chris

headlight.jpg (9.3 kB)
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
0

Answer by Griffo · Nov 24, 2012 at 06:03 PM

There might be a better way but you could do this -

 private var rightLight : Light;
 private var leftLight : Light;
 private var LightR : GameObject;
 private var LightL : GameObject;
 
 function Awake(){
 
     LightR = GameObject.Find("Right Light");    // Find the gameObject with the Right Light
     LightL = GameObject.Find("Left Light");     // Find the gameObject with the Left Light
     rightLight = LightR.GetComponent(Light);
     leftLight = LightL.GetComponent(Light);
 }
 
 function Update{
 
  function OnMouseDown(){
     if(rightLight.enabled){
          rightLight.enabled = false;
          rightLight.enabled = false;
     }
     else{
          rightLight.enabled = true;
          rightLight.enabled = true;
        }
     }
  }
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 chris83 · Nov 25, 2012 at 01:36 PM 0
Share

@Giffro: Thank you for your answer it works :). Do I need to put On$$anonymous$$ouseDown() in the Update function?

avatar image
0

Answer by greatestprez · Nov 24, 2012 at 06:20 PM

This should work:

 function OnMouseDown(){
     if(lightToToggle.light.enabled){
          lightToToggle.light.enabled = false;
          lightToToggle.transform.FindChild("Headlight").light.enabled = false; 
 
     }
     else{
          lightToToggle.light.enabled = true;    
          lightToToggle.transform.FindChild("Headlight").light.enabled = true; 
     }
  }
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 chris83 · Nov 25, 2012 at 01:38 PM 0
Share

Thank you greatestprez for your answer. Your Code is short and works fine for the given issue. You are using FindChild() therefor I assume that we have a parent-child situation?

avatar image greatestprez · Nov 26, 2012 at 02:55 AM 0
Share

yea you have a parent-child situation

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

12 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Help With "Scripting" 1 Answer

Help with selecting and deselecting an object? 1 Answer

Overlap Detection HELP!! 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