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
1
Question by Vumi · Jan 05, 2013 at 02:34 PM · 2djavascriptguiarray

What type of Array should I use?

I need a javascript 2D array to store information about GUI boxes to be rendered, for example.

alt text

The "boxTitle" along with a few other columns need to be strings, the xPosition, yPosition etc. Determine the position of each box and therefor need to be ints.

My question, how would I go about declaring this, what type of array should I use, and could you give me an example of how I would then read from the array?

I have spent a whole day trying to wrap my head around something that should be really easy.

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 Piflik · Jan 05, 2013 at 02:57 PM

Create a custom class with the needed attributes and create an array of that class.

 //Box.js:
 
 public class Box {

 public var boxID : int;
 public var boxTitle : String;
 public var xPosition : int;
 public var yPosition: int;
 public var xSize : int;
 public var ySize : int;

     function Box() {
         boxID = 0;
         boxTitle = null;
         xPosition = 0;
         yPosition = 0;
         xSize = 0;
         ySize = 0;
     }

     function Box(ID : int, title : String, xP : int, yP : int, xS : int, yS : int) {
         boxID = ID;
         boxTitle = title;
         xPosition = xP;
          yPosition = yP;
         xSize = xS;
         ySize = yS;
     }
 } 

Create an array like this:

 private var testArr : Box[] = new Box[10];   //use whatever number of entries you need in that array
Comment
Add comment · Show 6 · 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 Vumi · Jan 05, 2013 at 03:29 PM 0
Share

I think this will get me on my way but I do have another question, I am very new to all this sorry. Could you give me an example of how I would add an entry to the "testArr" array?

avatar image Piflik · Jan 05, 2013 at 03:42 PM 0
Share

You need to create the array with enough entries, because you can't resize it.

You would not add new entries, but replace existing ones, like:

 testArr[3] = new Box(1323, "testBox", 10, 40, 15, 45);   //values completely random

If you need to iterate through this array, and you are not sure that it is completely filled with valid Boxes, you can test the ID of each entry and only create/display a GUI.Box if the ID is not 0 (since the array is initialized with default Boxes and these have ID = 0).

Example:

 for(var i : int = 0; i < testArr.length; i++) {
     if(testArr[i].ID != 0) {
         //do stuff
     }
 }

If you need to change the number of entries dynamically, you could use a list ins$$anonymous$$d of the array.

avatar image Vumi · Jan 05, 2013 at 04:24 PM 0
Share

Hmm, i'm getting an error using that line "testArr[3] = new Box(1323, "testBox", 10, 40, 15, 45);" it says: "The type 'Box' does not have a visible constructor that matches the argument list '(int, String, int, int, int, int)'."

avatar image Piflik · Jan 05, 2013 at 04:40 PM 0
Share

Ok, sorry for the confusion. Never used a custom class in UnityScript, so I had some mistakes in my code. First of all, UnityScripts are Subclasses of $$anonymous$$onoBehaviour by default, and they can't have constructors with arguments, so you need to specify that this class should not extend $$anonymous$$onoBehaviour...also constructors apparently don't have a return type in UnityScript. I updated the code above (the Box.js), and now it should work.

avatar image Vumi · Jan 05, 2013 at 04:58 PM 0
Share

That fixed most of the problem, although "testArr[i].ID" doesn't seem to reference the class. I get the error "NullReferenceException: Object reference not set to an instance of an object"

Thank you for all this help btw, I really do appreciate it.

Show more comments

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

10 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

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

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

Joystick ellipse movement (2d mobile asset modification) 1 Answer

Different results between "ContactPoint" and "ContactPoint2D" 0 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