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 amukhi123 · Jun 10, 2016 at 02:16 AM · c#array

Array Out Of Range C#

I am new to unity and my code below gives me the error; Array index is out of range. Could someone explain to me why this happens and the possible solutions. Thanks! I could post any other things necessary.

Inspector: alt text

Erroralt text:

Code:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnPrefab : MonoBehaviour {
 
     public GameObject[] enemies;
     public int amount;
     private Vector3 spawnPoint;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         enemies = GameObject.FindGameObjectsWithTag ("Apple");
         amount = enemies.Length;
 
         if (amount != 3) {
             InvokeRepeating ("spawnEnemy", 5, 10f);    
         }
     }
 
     void spawnEnemy() {
         spawnPoint.x = Random.Range (9, 6);
         spawnPoint.y = Random.Range (9, 6);
 
         Instantiate(enemies [UnityEngine.Random.Range(0, enemies.Length - 1)], spawnPoint, Quaternion.identity);
         CancelInvoke ();
     }
 }


temp.png (7.7 kB)
error.png (4.4 kB)
Comment
Add comment · Show 3
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 TBruce · Jun 10, 2016 at 02:19 AM 0
Share

Could you edit your post and place a comment in the code to let us know where the error is? Something like this

 //<---- error here
avatar image amukhi123 TBruce · Jun 10, 2016 at 02:23 AM 0
Share

I updated it

avatar image jlmakes · Jun 10, 2016 at 01:24 PM 0
Share

You are using GameObject.FindGameObjectsByTag() in Update() without any throttling, which is not so great for performance—Unity has to search through every object...every frame!

If you know which items should be in that array, it would be better to manually assign them in the editor by dragging the desired game objects to that component—or do it once in the Start() method.

If you don’t know which items should be in the array, and/or they do need to be assigned at runtime, you should look into some strategies for limiting how often Unity has to search.

2 Replies

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

Answer by TBruce · Jun 10, 2016 at 02:32 AM

Try this

 if (enemies.Length > 0)
 {
     if (enemies.Length == 1)
     {
         Instantiate(enemies [0], spawnPoint, Quaternion.identity);
     }
     else
     {
         // since 0 and enemies.Length are int values then enemies.Length is exclusive meaning you will get a result between 0 and (enemies.Length - 1)
         int index = UnityEngine.Random.Range(0, enemies.Length);
         Instantiate(enemies [index], spawnPoint, Quaternion.identity);
     }
 }

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 jlmakes · Jun 10, 2016 at 02:32 PM 0
Share

Take note!

Random.Range with type float has both inclusive lower and upper bounds.

Random.Range with type int has an inclusive lower bound, but exclusive upper bound.

So @$$anonymous$$avina is correct that Random.Range(0, enemies.Length) will provide the correct range.

avatar image
0

Answer by btmedia14 · Jun 10, 2016 at 01:14 PM

I noticed the following in your code sample. At line 20 :

          if (amount != 3) {
              InvokeRepeating ("spawnEnemy", 5, 10f);  

This implies spawnEnemy() is called even when amount is zero, i.e. when there are no enemies. When enemies[] array is referenced the index is undefined. Perhaps determine if this is the condition required, maybe amount >= 3 for example.

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

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

171 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Just another line renderer question. Line renderer vector positions in an array of game objects. My line only draws between 2 positions? 1 Answer

Class array initialization on Start() gives -> Object reference not set to an instance of an object 1 Answer

Cant define array from other script 0 Answers

Checking if an instance of a Script saved in 3d array is null 1 Answer

Best way to serialize a big array of a class 2 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