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
0
Question by MoradGM · Jul 25, 2013 at 03:36 PM · c#gameobjectlight

Accessing lights from c# code

Hello I have a scene setup, with five point lights named pl1, pl2...pl5. Now I am very new to Unity, and I would like that when the game starts I want the intensity of the point lights to be 0, and whenever the player controller gets a certain amount of distance from the light it would increase its' intensity.

Now I understand I have to do this: I created an empty game object which I named gameController, created a c# script, and in it I need to instantiate a Light array, and in the start method to set their intensity to zero, and just go on coding the change of intensity in the update method.

What I am having trouble with is how on earth do I access the lights in my code? I searched many posts and I couldn't find one straight answer.

I tried: Light[] lights = (Light)FindObjectsOfType(typeof(Light)); That doesn't work.

now I thought about creating a GameObject array, but then, how do I make sure it returns a light?

I would very much appreciate any help.

Thank you.

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

Answer by TK5005 · Jul 25, 2013 at 04:01 PM

 private Light[] lights;
 
     // Use this for initialization
     void Start () {
         lights = FindObjectsOfType(typeof(Light)) as Light[];
         foreach(Light light in lights)
         {
             light.intensity = 0;
             Debug.Log(light);
         }
     }

This would give you a list of all of the Lights in the game. If there are other lights, using FindWithTag() would be better.

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 MoradGM · Jul 28, 2013 at 03:25 PM 0
Share

Thank you, appreciated!

avatar image
0

Answer by wolfadex · Jul 25, 2013 at 03:55 PM

GameObject.light will give you access to the light.

For finding the GameObjects with lights, you could use GameObject.FindWithTag() and make sure your lights are tagged with something like 'light' or 'dimmingLight'.

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 sundayhd5 · Apr 23, 2020 at 12:17 AM

Light is a GameObject with a "Light" compoment. Therefore, you can find any light the same way you that you find GameObjects, Then you can get the light compoment from them and change light properties. For example:

 Light myLight = GameObject.Find("MyLight").GetComponent<Light>();
 // now we can change light properties...
 myLight.range = 5;
 myLight.intensity = 2;

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

18 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

gameObject are not referenced 2 Answers

Ordering a list of GameObjects 3 Answers

How to scroll progressively a game object? 0 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