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 Ian-McCleary · Apr 14, 2014 at 02:28 AM · javascriptarrays

Array index is out of range (Javascript)

OK, so i have this simple inventory script and for some reason i get this error when i hit the play button and then try to click.

IndexOutOfRangeException: Array index is out of range. Inventory02.Update () (Assests/script/inventory02.js:11

From the error i assume it is on line 11 but there is nothing with an array on that line! I really need some help on this!

Error on line 11...i think

 #pragma strict
 
 var Player : Transform;
 var material : int;
 var Static : int = 1;
 var ItemTexture : Texture;
 var NewText : Texture[];
 
 function Start()
 {
 ItemTexture = NewText[material];
 }
 
 function Mining(Character : Transform)
 {
 Player = Character;
 Player.SendMessage("ADD",material);
 Player.SendMessage("ADDTexture",ItemTexture);
 Static -=1;
 }
 
 function Update()
 {
 if(Static <=0)
 {
 Destroy(gameObject);
 }
 }
Comment
Add comment · Show 6
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 getyour411 · Apr 14, 2014 at 02:37 AM 0
Share

Line 11 is most defintely an array. You are assigning ItemTexture to be the array element of NewText[material] where material is your int/array index.

http://unity3d.com/learn/tutorials/modules/beginner/scripting/arrays

avatar image ikelaiah · Apr 14, 2014 at 02:45 AM 0
Share

Perhaps, you have not assigned your NewText:Texture[] from the inspector. When you assigned Texture[] from the inspector, you should be able to access the array.

Further note, your code is really difficult for me to read.

 var Player : Transform;

I had to stop a little while to see which one is the variable and which one is the type. But, the following is a lot easier to read.

 var player : Transform;

 
avatar image Ian-McCleary · Apr 14, 2014 at 02:46 AM 0
Share

I dont understand why i am getting the error though, i made sure all textures were assigned in the inspector.

avatar image getyour411 · Apr 14, 2014 at 03:23 AM 0
Share

What's the value of (int) material?

avatar image Ian-McCleary · Apr 14, 2014 at 03:53 AM 0
Share

it is automatically set to 0 in the unity inspector... but i can change it if i want. Should it be the same as another variable?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by VIPINSIRWANI · Apr 14, 2014 at 09:01 AM

Once try with Replace line number 11 with ItemTexture = NewText[0]; and try.

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 VIPINSIRWANI · Apr 14, 2014 at 09:06 AM 0
Share

hi i add your script in my project its working fine here....Once restart unity.

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

23 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to store aspects (position, name, tag) of a game object in an array to be used for reinstantiation? 1 Answer

getting "null" when using array. (javascript) 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