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 Tatsunomi · Aug 01, 2013 at 01:54 AM · arraysindexoutofrangeexception

IndexOutOfRangeException when activating a component(follow-up question)

This is a follow up question from my previous question about index's. I am receiving an array index out of range exception when I instantiate a prefab. The code successfully gives me the index but I can't seem to activate/interact with the game Objects component. I receive the IndexOutOfRangeException. I don't want the array or list to be limited to a specific count; lets say up to 10 cause I have a Gui button that when I click it. It spawns that prefab I'm trying to interact with.

Here's the Sample Code: using UnityEngine; using System.Collections;

 public class IndexSample : MonoBehaviour {
         //Array list for identifying indexes
         //static ArrayList MovePlayer = new ArrayList();
         GameObject[] PlayersObject;
         int i;
         //End Array list
  
         RaycastHit hit;
         public Light myLight;
         public bool Selected = false;
  
  
  
            // Use this for initialization
         void Start () {
  
  
         }
         // Update is called once per frame
         void Update () {
             Ray ray = camera.ScreenPointToRay(Input.mousePosition);
             //Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
  
             if(Physics.Raycast(ray, out hit, 100))
              {
             //ARRAY Index implementation
             PlayersObject = GameObject.FindGameObjectsWithTag("Player");
             for(i = 0; i < PlayersObject.Length;i++)
             {
                 if(hit.collider.gameObject == PlayersObject[i].gameObject){
                     print (i);
                 }
             }
             //End
              print ("Here is an object!");
              if (hit.transform.gameObject.tag == ("Ground")){
               //Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
               print ("Ray has Hit the Ground");
              }
              else if (hit.transform.gameObject.tag == ("Player")){
               Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
               if ((Input.GetMouseButtonDown(0))&&(hit.transform.gameObject.tag == "Player")){
                   print("You Clicked the Player!");
                   //GameObject[] light = GameObject.FindGameObjectsWithTag("Player");
                   //myLight = light[i].GetComponent<Light>();
                   myLight = PlayersObject[i].GetComponent<Light>();
                   myLight.enabled = true;
                   Selected = true;             
               }
          else
          {
          }
     }
 }
     }
 }
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
0
Best Answer

Answer by robertbu · Aug 01, 2013 at 05:02 AM

The lack of correct indentation on this code makes it very difficult to read. Also it would have been very helpful if you'd pasted in the error message from the console.

Your error appears to be on line 46. On line 28, you cycle through a 'for' loop printing out your game objects. That loop terminates with 'i' is equal to PlayersObject.Length. Arrays are zero based. So the valid indices for a five element array is 0,1,2,3 & 4. So when the 'for' loop is done, 'i' is one beyond the valid indices for the array. You use this i-set-to-an-invalid-index on line 45 causing the error. Right now your 'for' loop ends on line 33. I'm guessing the code from line 35 to 47 should be inside the loop.

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 Tatsunomi · Aug 01, 2013 at 04:18 PM 0
Share

I already solved the error sir but thanks for the answer

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

15 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

Related Questions

Index is Less Than Zero + Flawed Code 1 Answer

Array index is out of range? 1 Answer

Array index is out of range 0 Answers

Attempt at creating an array of text files results in different errors 1 Answer

IndexOutOfRangeException error for array points in range 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