• 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 Tony_T · Oct 23, 2015 at 06:09 PM · arrayloopmultipleallaffect

Affect every object in array.

Hello. What I'm trying to do is disable all the lights i have selected in the Inspector, but since the number of lights may vary i can't use: [0], [1], [2] etc. I guess i have to use [Lights.length] but i get the error "Array index is out of range." when i run it maybe cause i haven't set the min of the array. Here is a part of my script if that helps.

 var LightSources : Light[];
 
 function Update ()
 {
 LightSources[LightSources.length].GetComponent.<Light>().enabled = false;
 }


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

1 Reply

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

Answer by Cherno · Oct 23, 2015 at 06:26 PM

Loop through the array like this:

 foreach(Light light in LightSources) {
         light.enabled = false;
  }

or

  for(int i = 0; i < LightSources.Length; i++) {
       Light light = LightSources[i];
       light.enabled = false;
 }

As for your error: Note that you try to access the elements at position (LightSources.Length). If LightSources.Length is, say, 5, then you try to access the element. as position 5 However, with a Length of 5, the element positions are 0,1,2,3,4, so 5 is outside the scope of the array. In any way, this would only affect this one element in the array, not all of them as you might have thought.

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 Tony_T · Oct 23, 2015 at 06:57 PM 1
Share

I'm not sure i need a loop for what i want to do. I just want to disable all the lights that's all. Ins$$anonymous$$d of using LightSources[0].GetComponent.<Light>().enabled = false; i want to use a line that will disable them all at once. Since i can't use LightSources[0, LightSources.length].GetComponent.<Light>().enabled = false; there must be something similar.

avatar image Eno-Khaon Tony_T · Oct 23, 2015 at 07:09 PM 1
Share

A loop is the means of making changes to multiple objects at once.

@Cherno has it right. You already have your array, so you simply need to change each object in the array.

That said, even if there were a function to modify all elements in an array simultaneously, on the backend, it would still have to go one-by-one, so it would have an identical computational cost.

Edit: Additionally, you can have an array of Lights to populate coinciding with your lightsources. For example:

 private var Light[] lights;

 function Start()
 {
     for(var i = 0; i < LightSources.length; i++)
     {
         lights[i] = LightSources[i].GetComponent(Light);
     }
 }

Then, in your Update() function, you can turn off the "lights" rather than getting the components back out of the "LightSources" every time.

avatar image roojerry Tony_T · Oct 23, 2015 at 07:43 PM 1
Share

I don't really recommend it, as a simple loop would be more readable and more efficient, but this fits your one line requirement

using System.Linq

 LightSources.ForEach(light => light.enabled = false);

still just a loop on the backend

avatar image Tony_T · Oct 23, 2015 at 08:04 PM 0
Share

Ahhh, I see now. It's working fine. I though it would be much easier, something to do with length but i guess i was wrong. Cheers guys !

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

33 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

Related Questions

Instantiate an array of prefabs? 0 Answers

Store multiple types of data in an Array? 2 Answers

Adding an object to an array only if it is not in the array 2 Answers

Count Builtin Array elements with a certain property. 1 Answer

How can I do a foreach loop for an array of booleans? 1 Answer

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