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 dot · Sep 24, 2010 at 09:34 PM · arrayoutofbounds

Array "size" question.

I have this variable

var scriptsNames: String[] = new String[0];

declared outside any function

now when I try to assign

function Start(){
scriptsNames[0] = "name1";
scriptsNames[1] = "name2";
scriptsNames[2] = "name3";
scriptsNames[3] = "name4";
}

Unity gives me errors. it can be fixed if I change, outside, in the Unity inspector http://img46.imageshack.us/img46/5198/gownon.png size, over here.

but I suppose it can be changed inside a script, somehow? Is anybody willing here to show me how to access it, and change it to 4, for example, using scripting?

Thank you in advance.

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
2
Best Answer

Answer by spinaljack · Sep 24, 2010 at 09:50 PM

http://unity3d.com/support/documentation/ScriptReference/Array.html

The difference between a Unity array and a java script array is that Unity arrays cannot be resized so unless you know the exact size of an array before run time you should stick with java script arrays.

To make one of those you type:

var array = new Array();

and then all the basic array functions can be carried out on it like push and pop.

To create a Unity array (which are faster) with a fixed size you type:

var unityArray: String[];

function Start(){ unityArray = new String[4]; }

You can switch between the two types using the array.ToBuiltin() function

P.S. It might not apply to you but I've noticed that creating large complex arrays may take more than one frame so trying to assign a value straight away sometimes leads to out of bounds errors (yes, sometimes..). To get around this I added an array check with a yield to wait for the array to finish initialising before assigning values to it.

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 dot · Sep 24, 2010 at 10:34 PM 0
Share

Allright man, thanks for that. Your answer led me straight to what I needed. Thank you!

avatar image Eric5h5 · Sep 25, 2010 at 12:01 AM 0
Share

I seriously doubt it's possible for arrays to take more than one frame to create. If it's not an atomic operation, Unity would be pretty broken.

avatar image spinaljack · Sep 25, 2010 at 02:24 AM 0
Share

I had an error where exactly this was true, it was an array of classes though which also contained arrays

avatar image spinaljack · Sep 25, 2010 at 02:33 AM 0
Share

Also, the classes were just a method for storing different data types together, they don't have any functions.

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

No one has followed this question yet.

Related Questions

Why is my Array going out of bounds?? 1 Answer

Find the average of 10 Vectors 4 Answers

JavaScript String Question 2 Answers

Instantiating random prefabs 1 Answer

Wrong object always Instantiated 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