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 MaorDany · Dec 31, 2017 at 05:34 AM · lightspotlight

all SpotLights on/off c#,Spotlight on/off C#

how to turn on and off all spotlights on scen and How to make it just for one spotlight (ForEach c#)?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lysander · Dec 31, 2017 at 07:06 AM

FindObjectsOfType(typeof(Light)) will return all active lights in the scene, and then it's just a matter of iterating over all of them and checking if their type == LightType.Spot if you're looking specifically for spotlights. Add them all to a collection and you can toggle them off and on as needed, but it's worth noting that it doesn't return inactive objects.

FindObjectsOfTypeAll(typeof(Light)) will grab all lights both in the scene and in prefabs in the Assets folder- it can be annoying having to sift through them just to find the scene lights, and it can be dangerous if you don't bother filtering and accidentally change asset files. It's also an extremely heavy operation in large scenes.

The best solution is to make a new collection of Light objects in a manager or singleton somewhere in the scene and manually drag each Light object into the collection in the inspector. This removes the need to use heavy Find functions- you can just iterate over the list when needed to enable/disable the light en-masse.

You can optionally make the collection hold object types that have int IDs, string Names, and the Light reference, instead of just the Light reference by itself, to be able to disable or enable specific lights by ID or Name.

 public class LightManager : MonoBehaviour
 {
     public class LightReference
     {
         public int id;
         public string name;
         public Light light;
     }
 
     public LightReference[] lightReferences;
 
     public void EnableAll()
     {
         foreach(var obj in lightReferences)
         {
             obj.light.enabled = true;
         }
     }
     // ETC...
 }

You could also make a new MonoBehaviour for the ID/Name combo to put on the light objects themselves, then make a collection of that type on the manager. This has the added benefit of each object maintaining its own identity, which is a plus, but it depends on what your specific needs are.

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 MaorDany · Dec 31, 2017 at 07:34 AM

Thanks. Why does this code not work for me?

 Light[] lights = FindObjectsOfTypeAll (typeof(Light));

 foreach(Light obj in lights)
 {
     if (obj is LightType.Spot) {
         obj.enabled = true;
     }
 }
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 Lysander · Dec 31, 2017 at 08:05 AM 0
Share

Please convert this to a comment ins$$anonymous$$d of an answer.

It's not working because "is" is special syntax for checking the System.Type of an object. obj is never of System.Type "LightType.Spot", it's of System.Type "Light". You need to check if obj.type == LightType.Spot ins$$anonymous$$d- obj.type there is a specific property for the Light object, and not referring to the System.Type. I know the wording is a bit confusing here.

     Light[] lights = FindObjectsOfTypeAll (typeof(Light));
      foreach(Light obj in lights)
      {
          if (obj.type == LightType.Spot)
         {
              obj.enabled = true;
          }
      }

Again though, using FindObjectsOfTypeAll without knowing how to filter scene objects from prefabs can be really dangerous. I really recommend using the drag-and-drop scene collection approach ins$$anonymous$$d.

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

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

Related Questions

2D game spotlight issue with floor tiles,2D game spotlight floor tiles get different brightness 0 Answers

Point lights and spot lights not baking in my scene 6 Answers

Spotlights don't have shadow? 1 Answer

Spotlights refuse to cast shadows. 0 Answers

Light not showing after iPhone build 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