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
1
Question by bobin115 · Sep 08, 2014 at 01:45 PM · lightinglighttoggle

lighting click help

im am new to coding and need help with my script what im trying to do is mouse down light on and mouse up light off here is the script

 function Update() {
 if (Input.GetKeyDown("Fire1")) {
     if (light.enabled == true)
        light.enabled = true;
     }
  
 if (Input.GetKeyup("Fire1")) {
     if (light.enabled == true)
         light.enabled = false;
     }
 }

i have tried adding in this:

 public var light : GameObject;

but i get this error can't add component "light" because it doesnt exist.check that the file name and class match

that didnt work so i tried

 var muzzleFlash : GameObject;
  
 function Update() {
  
     if (Input.GetKeyDown("Fire1")) {
            !linkedLight.enabled = linkedLight.enabled;
     }
 }

if someone could amend any my scripts and make them work that would be great

thanks

Comment
Add comment · Show 3
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 JacobHockey13 · Sep 08, 2014 at 01:55 PM 0
Share

Before doing anything change the first if (light.enabled == true) to this if (light.enabled == false)

avatar image Mayank Ghanshala · Sep 08, 2014 at 01:59 PM 0
Share

Drag and drop your Light game object in inspector to where you used this Script. I think you did't add that.

avatar image JacobHockey13 · Sep 08, 2014 at 02:01 PM 0
Share

If you attach this script to the light as a component you can refer to the light as gameObject and not have to declare variables.

3 Replies

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

Answer by ThePersister · Sep 08, 2014 at 03:04 PM

Well first of all (first if => false and GetKeyup => GetKeyUp):

 function Update() {
 if (Input.GetKeyDown("Fire1")) {
     if (light.enabled == false)
        light.enabled = true;
     }
  
 if (Input.GetKeyUp("Fire1")) {
     if (light.enabled == true)
         light.enabled = false;
     }
 }


Secondly, as Khenkel mentioned, light is already used as default, so try renaming it. Quick tip, you can use F2 to refactor the name, so you only have to do it once and the system will apply for the rest of the script :)

Thirdly, you can enable a "Light" component, right now you have a GameObject component, you can either deactive only the light component, or the entire gameObject. If you want to use the "enable" on a GameObject, you should use SetActive(true); (or false)

All of the Above leads to (Possibly):

 public var newNameForLight: GameObject;

 function Update() {
 if (Input.GetKeyDown("Fire1")) {
     if (newNameForLight.activeSelf == false)
        newNameForLight.SetActive(true);
     }
  
 if (Input.GetKeyUp("Fire1")) {
     if (newNameForLight.activeSelf == true)
         newNameForLight.SetActive(false);
     }
 }

OR

 public var newNameForLight: Light;

 function Update() {
 if (Input.GetKeyDown("Fire1")) {
     if (newNameForLight.enabled == false)
        newNameForLight.enabled = true;
     }
  
 if (Input.GetKeyUp("Fire1")) {
     if (newNameForLight.enabled == true)
         newNameForLight.enabled = false;
     }
 }

In both cases, don't forget to drag in the public variable :)

Hope that helps, if I misunderstood or made grammar mistakes, my apologies ;)

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
avatar image
1

Answer by bobin115 · Sep 08, 2014 at 03:12 PM

try this is simple and i think its what ur after

 function Update() {
   if (Input.GetMouseButtonDown(0)) {
     light.intensity = 1;
   }
   else if (Input.GetMouseButtonUp(0)) {
     light.intensity = 0;
   }
 }

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
avatar image
0

Answer by khenkel · Sep 08, 2014 at 02:01 PM

Try renaming the GameObject to something else, because "light" is already used. Every GameObject has a light variable by default (doesn't matter if it exists or not) and you're trying to access it.

Check this for more info: http://docs.unity3d.com/ScriptReference/GameObject-light.html

EDIT: Also if you want to disable/enable a GameObject, you have to use obj.SetActive(true/false), because obj.enabled doesn't work here.

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

26 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

Related Questions

How do I change a spot light to a point light by pressing a key? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Change something in a script with other script 1 Answer

lighting script help 1 Answer

light on collision help (scripting) 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