• 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 Martin 3 · Sep 24, 2010 at 08:06 AM · javascriptspotlight

Spotlight on/off

Hi, I want my character to have a flashlight, that they can turn on/off by hitting a key, and I dont know how to do it. I saw the same question, where they said to use this script:

var flashlight : GameObject;

var myLight : Light = flashlight.GetComponent("Light"); function Update() {

if (Input.GetKey("f")) { myLight.enabled = !myLight.enabled; }

}

The problem is that its in C#, and I dont know how to write the same code just in JavaScript. Can you help me with the script?

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

Answer by Marowi · Sep 24, 2010 at 08:29 AM

Looks like it's only the declaration that needs updating - note the syntax change.

(Note that the gameObject name is 'flashlight', not lightPoint - as per comments)

public var flashlight : GameObject;

function Start() { var myLight : Light = flashlight.GetComponent("Light"); }

function Update() { if (Input.GetButtonDown("Fire1")) { myLight.enabled = !myLight.enabled; } }

Let me know if there are any other problems - preferably the compiler error(s)!

Comment
Add comment · Show 6 · 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 Martin 3 · Sep 24, 2010 at 08:37 AM 0
Share

I get the compiler error BCE0005: $$anonymous$$ identifier: 'lightPoint'.

avatar image Marowi · Sep 24, 2010 at 08:48 AM 0
Share

That's because lightPoint is referring to a gameObject that should have been declared further up in the script. I can help you track it down, if you can't find it - just update your question with the full script.

avatar image Martin 3 · Sep 24, 2010 at 09:18 AM 0
Share

I'm new to the Unity, so I still have problems with it. Here is what I do:

var flashlight : GameObject; var myLight : Light = Flashlight.GetComponent("Light");

if (Input.GetKey("f")) { myLight.enabled = !myLight.enabled; }

in the Character object. I set the flashlight and mylight variable to the Flashlight, but it give me this code : Object reference not set to an instance of an object

avatar image Marowi · Sep 24, 2010 at 09:20 AM 0
Share

UnityScript (as with most languages) is case sensitive. If you declare it as "flashlight", you need to call it as "flashlight", rather than "Flashlight". I'll update my answer to use your script - your question calls it "lightPoint", which is why you got that error!

avatar image Martin 3 · Sep 24, 2010 at 09:31 AM 0
Share

Okay, it works now, but console still writes an error : Object reference not set to an instance of an object. Even that I had changed ALL the variables and the name of the spotlight to flashlight (with lowercase letters). I'll add the script as an answer to my own question.

Show more comments
avatar image
0
Best Answer

Answer by Martin 3 · Sep 24, 2010 at 09:32 AM

var flashlight : GameObject;

var myLight : Light = flashlight.GetComponent("Light"); function Update() {

if (Input.GetKey("f")) { myLight.enabled = !myLight.enabled; }

}

Comment
Add comment · Show 4 · 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 Marowi · Sep 24, 2010 at 09:35 AM 0
Share

For future reference, this should go in the Question field, not as a new answer.

avatar image Martin 3 · Sep 24, 2010 at 09:37 AM 0
Share

Forgot it was possible :D

avatar image Marowi · Sep 24, 2010 at 09:39 AM 0
Share

You can delete answers too ;)

avatar image AwsomeRod · Dec 14, 2012 at 02:56 AM 0
Share

sorry fixed it but it turns on and off fast... sometimes doesnt turn on/off Y

avatar image
0

Answer by Squeekpro107 · Apr 13, 2016 at 11:26 AM

I edited this so you can turn off and on the light feel free to use. :-)

 var flashlight : GameObject;
  
   
  var myLight : Light = flashlight.GetComponent("Light"); 
  
  
  function Update() {
  
   
      if (Input.GetKey("f")) { myLight.enabled = !myLight.enabled; 
      }
  
      else if (Input.GetKey("f")) { myLight.enabled = !myLight.enabled == false; 
      }
  
   
  } 

@GMills

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Luz parpadeante con bateria 0 Answers

How do i make a Spotlight able to turn on and off in Javascript? 2 Answers

Add delay to javascript? 1 Answer

Turning lights on or off with only 1 button 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges