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 /
  • Help Room /
avatar image
0
Question by Lore_NN · Nov 01, 2015 at 11:15 PM · scripting problemarraylights

Array index is out of range. How to solve?

Hi to all, I made this script with Unity 3 and It's works fine.. when I imported It in Unity 5 I get the error: Array index is out of range. How Can I solve this? This script make change differents point lights in a certain Time.. The console says that the errror is: lights[currentLight].enabled = false; (line 75)

have a good day ^^

  [System.Serializable]
         public class AnimazioneLuci : MonoBehaviour {
          
             // number of seconds between changing light
             public float lightTime = 1;
          
             // the current light that is lit.
             int currentLight = 0;
          
             public Light[] lights;
             public KeyCode controlKey = KeyCode.X;
             private bool on = false;
          
             void Update(){
     
             if (Input.GetKeyDown (controlKey)) {
     
             on = !on;
      
             if (!on) {
             StartCoroutine ("AccendiLuci");  
             StopCoroutine ("SpegniLuci");
     
             }
             }
      
             if (on) {
      
              StartCoroutine("SpegniLuci");
              StopCoroutine ("AccendiLuci");
     }
     }   
          
             IEnumerator AccendiLuci()
             {
          
                 yield return new WaitForSeconds(lightTime);   
          
                Debug.Log("Changing light to number "+currentLight);
          
                // disable the current light
                lights[currentLight].enabled = false;
          
                // change the index to the next light
                 currentLight++;
          
          
          
                // if the index is 4 or more, go back to the first light
                if (currentLight >= 4)
                  currentLight = 0;
          
                // the current light is now the next light, so enable it.
                lights[currentLight].enabled = true;
          
                // call this method again to create an infinite loop.
                StartCoroutine("AccendiLuci");
          
             }
          
         
         
         
         
         
         
                 IEnumerator SpegniLuci()
             {
          
                 yield return new WaitForSeconds(lightTime);   
          
                Debug.Log("Changing light to number "+currentLight);
          
                // disable the current light
                lights[currentLight].enabled = false;
          
                // change the index to the next light
                 currentLight++;
          
          
          
                // if the index is 4 or more, go back to the first light
                if (currentLight >= 4)
                  currentLight = 0;
          
                // the current light is now the next light, so enable it.
                lights[currentLight].enabled = false;
          
                // call this method again to create an infinite loop.
                StartCoroutine("SpegniLuci");
          
             }
          
         
         
         
         
         
          
         }

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 Lore_NN · Nov 01, 2015 at 01:01 PM 0
Share

Someone can Help $$anonymous$$e?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Nov 01, 2015 at 11:38 PM

Your lights array is too small. The code requires exactly 4 lights. Check the inspector - the array is most likely empty, 1, 2 or 3 lights. It requires 4.

Or change the code so it works with any amount of lights.

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 Lore_NN · Nov 02, 2015 at 07:39 PM 0
Share

I've alredy tried to put 4 lights, that error is removed, but I don't see the lights on..but I see that the lights in the inspector change.. Why? In the error bar there is this other message: Object reference not set to an instance of an object.. It says this is line is wrong.. lights[currentLight].enabled = false; With the unity 3 project i didn't have this problem..

Thank you for your support

avatar image Statement Lore_NN · Nov 02, 2015 at 08:45 PM 0
Share

Object reference not set to an instance of an object

Your array is missing at least one light. The array might be 4 in size, but at least one of the items in the array is null.

avatar image Lore_NN Statement · Nov 02, 2015 at 10:16 PM 0
Share

There are 5 lights in the inspector alt text

immagine.png (12.7 kB)
avatar image Statement Lore_NN · Nov 02, 2015 at 10:22 PM 0
Share

Are you sure the error is co$$anonymous$$g from that specific object? If you single click the error message, it should "ping" the object in heirarchy (It'll look like a yellow highlight that kind of pops out a little)

When the error happen at runtime, see if you still got all lights in the array (if one was destroyed, for example, it wouldn't work).

Also if you write to the array at runtime, make sure you write to it before your code access the array contents. Likewise, be wary of writing null values into the array at runtime.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Array index out of range. But it doesn't appear to be. 2 Answers

Object Pooling Spawn Script 1 Answer

Assign values to array elements 1 Answer

need help in randomising array 1 Answer

How to declare a list of arrays? 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