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 Ful · Nov 14, 2012 at 11:47 PM · javascripterrorarray

Texture2d[]: Array index is out of rang (Javascript)

Hello Everyone, I was wondering if someone could help me on fixing this error, I do realize that the reason of this error is that I didn't set a limit to the amount of arrays I'm using. However I have no idea on how to do so, but would highly appreciate if someone could help me. The max number arrays im currently using is 13.

Note: I just the arrays to stop after I press on the object 13th time, so basically a limit to 13 where no sound is play and etc...

Script:

 #pragma strict
 
 static var charge : int = 0;
 var swipe : AudioClip;
 
 // Calendar arrays
 
 var calCharge : Texture2D[];
 var calendar : Renderer;
 
 function Start () {
 
   charge = 0;
 
 }
 
 function Calendarchange () {
 
     AudioSource.PlayClipAtPoint (swipe, transform.position) ;
     charge++;
     calendar.material.mainTexture = calCharge[charge];
     print ("works");
 
 }
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 liszto · Nov 14, 2012 at 11:49 PM 0
Share

please use code sample balise and format all your code :/

And your charge value probably go over 13 use Debug.Log to check it's value ;)

And are you called this function (Calendarchange) ?

avatar image Ful · Nov 14, 2012 at 11:54 PM 0
Share

Yeah I do know that is happening because whenever I interact with the object (I'm using ray casting with Input.Get$$anonymous$$eyDown("e")) this occurs after I press the e key (after 13 times)

Yes I have another function that the ray cast is attached, the script is working but I have no Idea on how to set a limit to the array.

avatar image whydoidoit · Nov 15, 2012 at 12:05 AM 0
Share
 function Calendarchange () {
     if(charge >= calCharge.Length -2) 
            return;

    AudioSource.PlayClipAtPoint (swipe, transform.position) ;
     charge++;
     calendar.material.mainTexture = calCharge[charge];
     print ("works");

  }

3 Replies

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

Answer by whydoidoit · Nov 14, 2012 at 11:57 PM

If you want them to loop you should try calCharge[charge % calCharge.Length] (that's a modulus so its always inside the length of the array.

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 Ful · Nov 15, 2012 at 12:02 AM 0
Share

I just want them to stop after I press on the object 13th time, so basically a limit to 13 and then I don't want any sound and etc...

Thank you!!! its working

avatar image
0

Answer by Eric5h5 · Nov 14, 2012 at 11:58 PM

Use Array.Length and make sure "charge" does not equal or exceed it.

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 Ful · Nov 15, 2012 at 12:02 AM 0
Share

hm.. you mean using something like arr.length = 5;?

avatar image
0

Answer by OtsegoDoom · Nov 15, 2012 at 12:36 AM

If you're getting the "Array index is out of range" error then your [charge] variable must be going over the Length of your calCharge array. You'll need to add some code to check that charge++; doesn't go outside the Length (or size as it's called in the Inspector) of your array.

 function Calendarchange () {
 
     if ( charge < calCharge.Length ) {
         AudioSource.PlayClipAtPoint (swipe, transform.position) ;
         charge++;
         calendar.material.mainTexture = calCharge[charge];
         print ("works");
     }
 }

Adding this if statement will check to see if charge is less than the Length of your calCharge array. If it is, it will let the function continue. If not, it will do nothing. You can easily add in some code to do something else in that case.

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 Ful · Nov 15, 2012 at 12:46 AM 0
Share

Thank you for your solution however I've already been able to solve it. Anyway thanks again! this solution would definitely have as well worked.

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

13 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

Related Questions

IndexOutOfRangeExeption - Array index is out of range 2 Answers

Create array of children in Start function, use it in Update? 1 Answer

GetComponents array in C# error ? 4 Answers

for loop error 2 Answers

expected. Insert a semicolon at the end. When the end that it says, is a } Could someone help us fix this? 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