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 Maya84 · Nov 13, 2014 at 02:14 PM · arrayarraylistjavascript arrays

Can anyone help me with a system that helps me to access data inside of data?

I followed lessons on codeacademy in Javascript but they don't seem to apply in Unity. What I would like to achieve is accessing data inside of data like to following example:

var array01 = [1,2,3,4]; var array02 = [5,6,7,8,9,10]; var arrayBoth = [array01, array02];

// I would like to acces data with one command like:

arrayBoth[1][2];

Can anyone help me with a system that helps me to access data inside of data?

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 fafase · Nov 13, 2014 at 02:21 PM 0
Share

Unity does not use Javascript like the one you learn. try the learn section from Unity

avatar image CHPedersen · Nov 13, 2014 at 02:23 PM 0
Share

Ha, ninja-closed my reply. ;)

2 Replies

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

Answer by CHPedersen · Nov 13, 2014 at 02:20 PM

codeacademy's JavaScript tutorial is for Web JavaScript, which is not at all the same as Unity's poorly named JavaScript, also sometimes referred to as "UnityScript" to clear the confusion. Their tutorial does not apply whatsoever to Unity, save perhaps superficial syntax familiarity.

You declare, instantiate and access arrays in UnityScript like so:

 // Array declaration:
 var arrayExample : int[];

 // Array instantiation:
 arrayExample = new int[10];

 // Array access:
 var fifthElement = arrayExample[5];


Example of rectangular array syntax in JS:

 // Declaration of rectangular array:
 var rectArray : int[,];
 
 // Instantiate the rectangular array
 rectArray = new int[3,3];
 
 // Set the value of an element inside the array (at column = 2, row = 2) to 10
 rectArray[2,2] = 10;

 // Access the element at that position again and store it in a local variable:
 var twoPointTwo = rectArray[2,2];

 Debug.Log("Value of element at 2,2: " + twoPointTwo);
Comment
Add comment · Show 4 · 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 CHPedersen · Nov 13, 2014 at 03:07 PM 0
Share

I have updated my reply with sample syntax that applies to rectangular arrays.

avatar image Maya84 · Nov 13, 2014 at 03:45 PM 0
Share

Thanks! It works! What would be the changes if I want store strings ins$$anonymous$$d of numbers? Changing just int to string and 10 to "monkey" doesn't do the trick.

avatar image CHPedersen · Nov 14, 2014 at 08:41 AM 0
Share

Actually, doing exactly that works just fine. But UnityScript does not share C#'s lower case "string" keyword alias, so in UnityScript, you have to write the actual object name, which is with a capitalized S, i.e. "String", not "string". This is the same code but for strings:

 // Declaration of rectangular array:
 var rectArray : String[,];
 
 // Instantiate the rectangular array
 rectArray = new String[3,3];
 
 // Set the value of an element inside the array (at column = 2, row = 2) to "monkey"
 rectArray[2,2] = "monkey";
 
 // Access the element at that position again and store it in a local variable:
 var twoPointTwo = rectArray[2,2];
 
 Debug.Log("Value of element at 2,2: " + twoPointTwo);
avatar image Maya84 · Nov 18, 2014 at 09:45 PM 0
Share

Thank you very much for taking the time to answer my questions. Hopefully one day I will be good enough to help others as well the way you do!

avatar image
0

Answer by Maya84 · Nov 13, 2014 at 02:50 PM

Thanks for the fast reply! however It still doesn't work for me. I tried your code inside of Unity and both Debug.Log's don't output what I want(number 2).

 var arrayExample : int[];
 
 arrayExample = new int[3];
 arrayExample = new int[2];
 
 var secondElement = arrayExample[1];
 
 Debug.Log(secondElement);
 Debug.Log(arrayExample[1]);

The classic javascript way allows me to acces single items of the array. (I'm sure that it can be done with your way two). But what I really want is to access data that is inside a array in a array(see my example on top) Hope this make sence!

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 CHPedersen · Nov 13, 2014 at 02:56 PM 0
Share

You're fundamentally misunderstanding the syntax. In plain English, what you have in the above is:

A declaration of a variable (arrayExample) of type int array.

An instantiation of that variable to an int array with length 3.

A re-instantiation of that variable to a new int array, now with length 2. This overwrites the first instantiation.

Accessing the element at the second (zero-indexed) position. This number is 0, because you didn't assign any actual data to the array yet, you just declared their sizes.

Debug.Logs of this local variable, plus (again) the second element, which is still 0.

You need to read up on arrays here and fully understand their syntax before worrying about your question's actual topic which is called "rectangular arrays" (arrays of arrays):

http://docs.unity3d.com/ScriptReference/Array.html

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ArrayLists giving wrong information? C# 2 Answers

get int based on 3 ints 0 Answers

Help with for loop and arrays 3 Answers

How to acces the value of List of arrays 0 Answers

GameObject to Array/List or whatever fits best 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