• 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 Wenon · Jun 15, 2018 at 09:29 AM · arrayindexout of range

Array index out of range after build

Hello, In editor works good but after build there is error IndexOutOfRangeException: Index was outside the bounds of the array.

                 int rand = Random.Range(0, (cars.Length));
                 vehicle = Instantiate(cars[rand]);

I tried with cars.length-1 too but not working. There is only one element in cars array but will be more but at this time I want to make it works with one element in the array. Why in editor it works but not after build?

Comment
Add comment · Show 9
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 Hellium · Jun 15, 2018 at 09:34 AM 0
Share

Are you sure cars.Length is greater than 0 in the build? How do you populate the array? Can you:

  1. Add a Debug.LogError(cars.Length); before calling Random.Range

  2. Open your build settings

  3. Check the Development build option

  4. Build your game

  5. See what the console outputs

avatar image Wenon Hellium · Jun 15, 2018 at 09:53 AM 0
Share

outputs 0, but why, arrays have prefabs, they do not reset when the game is turned on in editor, why do they reset after build?

avatar image Feelnside Wenon · Jun 15, 2018 at 11:24 AM 0
Share

Try to move your prefabs into the "Assets/Prefabs" folder. $$anonymous$$aybe they are located in some unexpected folder and as result they do not included into the final build.

Show more comments
Show more comments
avatar image pcdrive · Jun 15, 2018 at 10:27 AM 0
Share

Can it happen, that the array is not existing at all? I run into same problems like, you create the variable like this: 'GameObject[] cars', and then the program goes a way where it gets no value at all. since its an array it will let you use the [] call, but throws an exception even for the 0 value.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wenon · Jun 15, 2018 at 05:04 PM

Please can anyone help me with arrays which resets after build with no reason?

Comment
Add comment · 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.

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

103 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

Related Questions

IndexOutOfRangeException: Array index is out of range. 0 Answers

Array of transform index out of range error 1 Answer

"Array index is out of range" for any index value, when array.length>index 2 Answers

How do I check a List[0], when it's nothing? 1 Answer

Array index is out of range Error, only when array is used to Instantiate 4 Answers

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