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 btiger · Jul 22, 2010 at 02:21 PM · arrayindexoutofrangeexception

Array index is out of range

Hi, I have these code, but always get "Array index is out of range" error.

var vertices : Vector3[] = new Vector3[10];

function Update () { if(Input.GetKeyDown(KeyCode.I)){ vertices[0] = new Vector3( 1, 1, 1); } }

can someone help me to solve this problem?? thanks.

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 rafeeplaymc · Jan 27, 2017 at 11:13 AM 0
Share
    gear$$anonymous$$axValue = gearRatio[i];

This is the line that shows array index out of range. This is the section of code where it is

 //Engine Sound
 
 function EngineSound(){
 for (var i = 0; i < gearRatio.length; i++){
 if(gearRatio[i]> currentSpeed){
 break;
 }
 }
 var gear$$anonymous$$inValue : float = 0.00;
 var gear$$anonymous$$axValue : float = 0.00;
 if (i == 0){
 gear$$anonymous$$inValue = 0;
 }
 else {
 gear$$anonymous$$inValue = gearRatio[i-1];
 }
 gear$$anonymous$$axValue = gearRatio[i];
 var engine$$anonymous$$ch : float = ((currentSpeed - gear$$anonymous$$inValue)/(gear$$anonymous$$axValue - gear$$anonymous$$inValue))+1;
 GetComponent.<AudioSource>().pitch = engine$$anonymous$$ch;
 }

3 Replies

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

Answer by Mike 3 · Jul 22, 2010 at 02:31 PM

Make the array private

Unity will use the inspector version of the array for any public arrays, and since you probably didn't fill values in there, it'll assign a 0 length array

private var vertices : Vector3[] = new Vector3[10];
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 btiger · Feb 08, 2011 at 10:06 AM 0
Share

Thanks, it works.

avatar image
5

Answer by PatHightree · Jul 22, 2010 at 11:13 PM

Don't initialize class variables at declaration time, doing it in Awake() or Startup() is the proper way to do it.

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

Answer by sikha · Feb 12, 2012 at 01:19 PM

I have same problem when use followin code on Player

var healthTex: Texture2D[]; // define the size and fill its elements in the Inspector

static var LIVES = 6;

var healthImage: Texture2D;

var healthImageOffset = Vector2(0, 0);

var nativeVerticalResolution = 1200.0;

function Update(){

 guiTexture.texture = healthTex[LIVES];

 DrawImageBottomAligned( healthImageOffset, healthImage); 

}

function DrawImageBottomAligned (pos : Vector2, image : Texture2D)

{

GUI.Label(Rect (pos.x, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image);

}

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

IndexOutOfRange Issue 1 Answer

IndexOutOfRangeException C# 1 Answer

Array index is out of range 0 Answers

What means this error ? 1 Answer

IndexOutOfRangeException: Array index is out of 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