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 DFLY · Mar 06, 2010 at 03:56 PM · lighttoggle

A simple light switch

Hi I am very new to scripting

So Please treat me like an idiot. I won't mind.

I just want to make a light switch on a box on a wall that when clicked with a mouse toggles of and on a light globe on a ceiling.

I have managed to figure out how to mouse click and toggle variables. But how do I send the on off command to the light script?

For that matter how do I run functions in other scripts/objects from a different object?

I have spent hours and hours searching the references and help and forum and the only answers I found may as well have been written in zulu.

WHAT I'VE DONE SO FAR.............

//Add a toggle variable for light on off call it lightState static var lightState : boolean;

function Update () {

// Make sure the user pressed the mouse down if (!Input.GetMouseButtonDown (0)) return;

     var hit : RaycastHit;



if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), hit, 100))

{

 lightState = (!lightState);
 print ("var lightstate = " + lightState);
     } 

     else {
 print ("I've clicked nothing!");

}

}

Thanks

Daivd

Also, is there a good book on javasript relevant to unity that I should get?

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

3 Replies

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

Answer by duck · Mar 08, 2010 at 10:43 AM

While raycasting is a very valuable thing to learn about when using unity, you could implement clickable light switches in a much simpler way. Place this script on each lightswitch object:

var linkedLight : Light;

function OnMouseDown() { linkedLight.enabled = !linkedLight.enabled; }

That is it. Now just drag that script onto each lightswitch. Once you have that script on each lightswitch, you need to drag a reference to the corresponding light into the "light" variable for each switch. This creates a reference link to each switch's light.

Note that you can have more than one switch per light using this method.

For more information about how to communicate between scritps and other objects, see this answer:

Communication between objects and other scripts

Comment
Add comment · Show 3 · 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 DFLY · Mar 08, 2010 at 12:46 PM 0
Share

Thanks that was a beautiful solution. You are a Legend. That was sooooo easy. I had read about dragging, but didn't understand until I followed your instructions.

Thanks a million.

avatar image cobair · Nov 29, 2012 at 10:39 PM 0
Share

"Once you have that script on each lightswitch, you need to drag a reference to the corresponding light into the "light" variable for each switch."

Can you please explain what this means for a total noob?

avatar image ozdev · Jul 03, 2013 at 02:24 PM 0
Share

thanks a lot, simple yet brilliant answer!

avatar image
0

Answer by Jito Jorge · Aug 04, 2012 at 12:47 PM

Hello !

I have another question. I try to invert the code because I want to turn off one light, and when this light is off, turn too another light on.

BUT .enabled doesn't exist and I try to invert the code like this

 var linkedLight : Light;

 function OnMouseDown() {
     !linkedLight.enabled = linkedLight.enabled;
 }

but is not good.

Do you have any solution ?

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 ScroodgeM · Aug 04, 2012 at 12:49 PM 0
Share

ask questions as new, don't ask in already answered or closed.

avatar image
0

Answer by m-bot · Dec 02, 2012 at 09:55 AM

try this for two lights:

 #pragma strict
 
 var linkedLight1 : Light;
 var linkedLight2 : Light;
 
 function OnMouseDown() {
 
  linkedLight1.enabled = !linkedLight1.enabled;
  linkedLight2.enabled = !linkedLight2.enabled;
 
 }
 

It work! I tested it.

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

6 People are following this question.

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

Related Questions

lighting click help 3 Answers

Adding lights and cameras to scenes with cSharp script 2 Answers

How do I initialize a simple light in C#? 2 Answers

Change something in a script with other script 1 Answer

Is there a way to measure light exposure for a specific point in the game world that can be used in scripts? 2 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