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 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
4
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.

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

OverlapSphere for parallel arrays 1 Answer

Running multiple coroutines in loops with different time 2 Answers

How to correctly shorten this script using arrays and iterations 2 Answers

Checking an array variable in C# vs JavaScript 3 Answers

Cycle Through Array of Enemies 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