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 Sketchwhale · Feb 11, 2013 at 04:54 PM · arraysstringbooleans

What are the built-in array syntax rules in this example...

I'm trying to understand the individual pieces of a tetris clone I found (to the original maker, thank you! I can't find the original anymore...) I'm trying to understand the individual parts so I can recreate something similar. I've removed various parts. What's left works together with a different script which is attached to an empty. If I press spacebar in that script, it instantiates another empty, which has this script attached.

This script is supposed to gather a number of cube shaped primitives (which it successfully does), in the shape that the block string array designates, trough a table of 1s and 0s in the inspector. As a tetrimino it's like this: oooo 1111 oooo oooo

What I don't understand, as I seem unable to find documentation for the syntax, is how, in the "if" statement", the information in the parenthesis is interpreted. How do the temporary variables x and y, correspond to the grid of 1s and 0s in the built-in array in the inspector? What are the syntax rules for making built-in arrays in the code example?

If I'm missing some vital information, please let me know :)

And just to make sure I'm clear: I didn't make this script, I'm just trying to understand it. Not trying to steal or take credit from anyone.

 var block : String [];
 
 private var size : int;
 private var halfSize : int;
 private var halfSizeFloat : float;
 
 function Start () {
     // Sanity checking. 
     size = block.Length;
     
     halfSize = size/2;
     halfSizeFloat = size*.5; // halfSize is an integer for the array, but we need a float for positioning the on-screen cubes (for odd sizes)
     
     // Convert block string array from the inspector to a boolean 2D array for easier usage
     for (y = 0; y < size; y++){
         for (x = 0; x < size; x++){
             if (block[y][x] == "1"){
                 var block = Instantiate(Button.use.cube, Vector3(x-halfSizeFloat, (size-y)+halfSizeFloat-size, 0.0), Quaternion.identity) as Transform;
                 block.parent = transform;
             }
         }
     }
 }
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
0

Answer by flaviusxvii · Feb 11, 2013 at 05:47 PM

block is an array of strings. Arrays and strings are both indexable with the square bracket syntax.

The expression

block[y][x]

Will give you the xth letter of the yth string..

Comment
Add comment · Show 2 · 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 Sketchwhale · Feb 11, 2013 at 07:10 PM 0
Share

That went a little above my head. Y and x seem to be just temporary variables, which are undefined in relation to block, so how do they relate to the elements in my array?

Sorry, I'm such a pain.

avatar image flaviusxvii · Feb 11, 2013 at 09:26 PM 0
Share

over the course of the nested loops, y and x will have the values (0, 0), (0, 1), (0, 2), ...(0, size-1), (1, 0), (1, 1), (1, 2)... and so on.

If block was 3 strings, ["Apple", "$$anonymous$$oney", "Larch"], at the point of the loop when when y = 1 and x = 3, then block[1][3] would be 'e'. Arrays offset at zero, so it's the second string and the fourth character.

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

My custom function for Updating a data string in an array causes deletion of all other data 0 Answers

Array of Array 1 Answer

Removeat with Strin[] 2 Answers

Issues with string array taken from a .txt file 2 Answers

ios cant access variables in array 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