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
0
Question by supergula · Sep 02, 2017 at 04:28 PM · lightcomponenttoggle

Toggle Light Component On and Off

I just watched the tutorial on toggling components on and off and while successfully toggling an object's renderer component, I can't toggle a light's light component

I used the exact same code as in the tutorial, but it just won't work...

Comment
Add comment · Show 1
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 Xepherys · Sep 02, 2017 at 04:34 PM 0
Share

Are you getting any exceptions? Particularly NREs, and maybe the variable isn't being assigned the light component?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by aidengaydos7 · Sep 02, 2017 at 04:34 PM

Is The Script On The Light? If So This Should Work. If Not You Need To Use:

 using UnityEngine;
 using System.Collections;
 
 public class EnableComponents : MonoBehaviour
 {
     private Gameobject lightObject;
     private Light myLightComponent;
     
     
     void Start ()
     {
         lightObject = Gameobject.Find("<Insert Objects Name>")
         myLight = lightObject.GetComponent<Light>();
     }
     
     
     void Update ()
     {
         if(Input.GetKeyUp(KeyCode.Space))
         {
             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 supergula · Sep 02, 2017 at 05:13 PM 2
Share

Thanks for the reply, I just found out I made a newbie mistake, I wrote "void start" and "void update" with lower cases ins$$anonymous$$d of upper cases :P

avatar image Julie_Karasch · Jan 27, 2018 at 02:40 PM 0
Share

How would I need to change the code so it is not working on a particular gameobject, but rather gameobjects with a specidic Tag?

avatar image Xepherys Julie_Karasch · Jan 28, 2018 at 02:50 AM 0
Share

Just initialize lightObject as a GameObject[] array, in Start() set it using a find by tag, then in update use a loop to loop through the array.

avatar image Julie_Karasch Xepherys · Jan 29, 2018 at 11:43 AM 0
Share

Thanks! I tried implementing it, but since I'm quite new to program$$anonymous$$g, I so far haven't worked with arrays. It seems there is something missing/wrong in my script in the start section:

void Start() { Collectable = GameObject.FindGameObjectsWithTag("Collectable"); myLightComponent = GameObject.FindGameWithTag("Collectable").GetComponent(); }

I got TestCubes with each a light component. cube1 can be lighted on and off with a push of a button. if i do the same on cube2, it turns on and off the light of cube1, not the light on itself. xD ? What is it I didn't get? ^^

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

73 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 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 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

GameObject with DirectionalLight component not being iluminated 1 Answer

Enable Draw Halo in Light Component via Script 1 Answer

How to turn the shadow of a light on and off 2 Answers

Toggling the light with lightmapping(baked and dynamic shadows) 1 Answer

Toggling Component's Function on a gameObject 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