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 DrakeDiablo · Aug 30, 2014 at 03:00 AM · guierrorarraybuttons

Array out of range!

Hello!

im trying to make a script that shows amount of buttons(the amount depends on the array). So if I enter 2 for Skills, there will be two buttons on the screen, this is just a start to it, so I havent actually added, a button adder thing, but right now I should have 1 button on my screen, but I dont, it always give me an error "Array out of range" no matter what I do. What should I do? Thanks

 public var SkillPos : Rect[];
  public var Index : int;
  public var Skills : int[];
  public  var Names : String[];
 
 
 function Start () {
 
 }
 function Update () {
 
 }
 
 function OnGUI () {
     GUI.Button(SkillPos[Index], Names[Index]);
 }
 
Comment
Add comment · Show 2
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 everett24 · Aug 30, 2014 at 08:01 PM 0
Share

Well is either array length set to 0?

avatar image AlwaysSunny · Aug 31, 2014 at 12:49 AM 0
Share

If this is the only code you've got, both the SkillPos and Names arrays are empty. Thus trying to access any of their elements will throw this error.

It's best to know the size of an array prior to creating it. Furthermore, in situations which require arrays to grow or shrink, a List is almost always preferable. Not sure if lists are available in JS, but you should consider switching to C# regardless - it's far superior.

1 Reply

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

Answer by BMayne · Aug 31, 2014 at 01:29 AM

Hey There,

Your arrays have no size and you are looking for the first element. Think of it this way.

 string myArray[3]{ "First", "Second", "Third" }

Here is an array with three elements. If I try to access element zero like you do above you will get the following result.

 Debug.Log(myArray[0]);
 
 
 --------- Fake Log ---------
  First
 ----------------------------

Now if I set the array like you did you will get this

 string myArray[];
 
 
 Debug.Log(myArray[0])
 
 --------- Fake Log ---------
 Out of range error!
 ----------------------------


You can't use whats not there :)

I hope that helps.

Regards,

Comment
Add comment · Show 5 · 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 DrakeDiablo · Aug 31, 2014 at 09:25 PM 0
Share

Hey, thanks for replying, I changed my code again and same problem...

 #pragma strict
 
 public var SkillsNum : boolean[];
 public var SkillsPos : Rect[];
 public var SkillsNames : String[];
 public var SkillsTexture : Texture2D[];
 
 
 function Start () {
 
     SkillsPos = new Rect[1];
     SkillsNum = new boolean[1];
     SkillsNames = new String[1];
     SkillsTexture = new Texture2D[1];
     
     SkillsPos[0] = Rect(5,5,5,5);
 
 }
 function Update () {
 
 }
 
 function OnGUI () {
     for(var i : int = 1; i <= SkillsNum.length; i++){
         SkillsNum[i] = GUI.Button(Rect(52 * i, 10, 50,50),"");
         if(SkillsNum[i]){
             
         }
     }
 }
 
avatar image radiKal07 · Aug 31, 2014 at 09:30 PM 0
Share
  for(var i : int = 1; i <= SkillsNum.length; i++)


This code should be:

  for(var i : int = 0; i < SkillsNum.length; i++)

Arrays start from "0" and go to "lenght - 1".

avatar image DrakeDiablo · Aug 31, 2014 at 09:36 PM 0
Share

I just changed it to 0, still says array index out of range :(

avatar image radiKal07 · Aug 31, 2014 at 09:42 PM 0
Share

Change it to 0 and make sure it goes to "i < SkillsNum.length" not "i <= SkillsNum.length". Notice ! To "<" not "<=".

avatar image DrakeDiablo · Aug 31, 2014 at 09:45 PM 0
Share

YAY!!! man ure the best, thanks for help!! one question, do you know where can i find tutorials on this stuff? because I need to make buttons appear in certain position on the screen, and that position depends on the rect array. :D thanks again!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Arrange ints from biggest to smallest and display them in a table 1 Answer

Bringing window to back GUI.BringWindowToBack() 0 Answers

GUISttyle NullReferenceException error. what am I missing ? 1 Answer

GUI error scripting help! 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