• 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
Question by rayhel · Feb 07, 2013 at 02:21 PM · booleanonmousedownlightsgui button

How to turn on/off multiple lights using GUI buttons?

Hi everyone,

I'm making a sample test on switc$$anonymous$$ng an on/off multiple lights using 1 GUI button. Right now I'm using javascript because I'm still learning unity. Could anybody give me some sample codes that I' could use. Thanks.

Comment

People who like this

0 Show 2
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 Landern · Feb 07, 2013 at 02:23 PM 0
Share

You need to describe your setup, are all the lights children of any thing? Are they prefabs, are they named, are they attached to the camera? Are you holding references to them in the script that is meant to disable(turn off/on) them?

avatar image rayhel · Feb 08, 2013 at 01:52 AM 0
Share

Hi Sir,

In my scene, I have 1 main camera, 2 point lights, and a plane. The lights are name Point Light 1 and Point Light 2, and the script is attached to the point lights which will be the one to control them at the same time. I want to use a gui button so that when I click the gui it will turn on/off those two lights.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Berenger · Feb 07, 2013 at 02:47 PM

That should do it.

 var lights : Light[];
 
 function OnGUI()
 {
     var on = lights[0].enabled;
 
     if( GUI.Button( Rect( 10, 10, 100, 100 ), "Turn " + (on ? "off" : "on") + " the lights !" ) )
     {
         for( var i = 0; i < lights.Length; i++ )
             lights[i].enabled = !on;
     }
 
 }
Comment

People who like this

0 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 rayhel · Feb 08, 2013 at 01:48 AM 0
Share

Hi sir,

When I try to put the codes on to my unity, this errors showed up:

Assets/Scripts/Control_Lights.js(7,39): BCE0043: Unexpected token: 100.

Assets/Scripts/Control_Lights.js(7,94): BCE0043: Unexpected token: ).

Assets/Scripts/Control_Lights.js(9,9): BCE0043: Unexpected token: for.

Assets/Scripts/Control_Lights.js(9,12): UCE0001: ';' expected. Insert a semicolon at the end.

Assets/Scripts/Control_Lights.js(9,14): BCE0043: Unexpected token: var.

Assets/Scripts/Control_Lights.js(9,18): BCE0044: expecting ), found 'i'.

Assets/Scripts/Control_Lights.js(9,48): BCE0043: Unexpected token: ).

Assets/Scripts/Control_Lights.js(13,1): BCE0044: expecting EOF, found '}'.

Is there anything that I do wrong? But anyways, thanks for helping me on my script.

avatar image Berenger · Feb 08, 2013 at 11:11 AM 0
Share

Well, coding on the fly is risky business. I should have tested it first Anyway, I forgot a some parenthesis in the if line. I fixed the code.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

A node in a childnode? 1 Answer

How do I make my textlabel stay after the user clicks on a structure? 1 Answer

How can I control a bool flag or two the colors change and the rotation on/off ? 0 Answers

Changing Missile Speed 1 Answer

Weird Boolean Problem c# 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