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 IMTRIGGERHAPPY9 · Nov 10, 2011 at 05:44 AM · arrayformulti

Step through an array and get the GameObjects next to it

ok so i have a gridFloor that was created with nested for loops and an instantiation, it was then put into a multidimension array. i was wonder how i would go about getting the variables around the current gameObject, but stop it when it hits a variable that is out of range, everything i have tried hasn't been working. here is what i have so far.

 for(var i : int = 0; i < findTotalRows.gridX; i++)
 {
     
     for (var j : int = 0; j < findTotalRows.gridY; j++)
     {
         
         if(floorMap.map[i,j].name == this.name)
         {
             
             myX = i;
             myY = j;
                 
             yNext = myY + 1;
                             xNext = myX + 1;
                             yBack = myY - 1;
                            xBack = myX - 1;
             tileUp = floorMap.map[myX,yNext];
             tileDown = floorMap.map[myX,yBack];
             tileLeft = floorMap.map[xBack,myY];
             tileRight = floorMap.map[xNext,myY]; 
         }
     }
     
 }

I am pretty sure this is not very Programmer minded (if that makes sense), but i am an artist coding :) i would like to do it this way as it makes the most sense in my mind and i have visions of doing other things with it doing it this way

Comment
Add comment · Show 9
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 IMTRIGGERHAPPY9 · Nov 10, 2011 at 09:54 PM 0
Share

anybody???

avatar image syclamoth · Nov 10, 2011 at 10:14 PM 0
Share

Hey, I'm an artist who ended up coding, and look at me now...

avatar image jahroy · Nov 11, 2011 at 12:03 AM 1
Share

Syclamoth's code is using the Clamp function to make sure the value of xNext is always between zero and the number of elements in the map.

http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$athf.Clamp.html

Another way you could do it would be to use the % operator (aka the modulus operator). This operator returns the remainder when you divide two numbers. You could use it like this:

 var x$$anonymous$$ax = 100;
 
 var theIndex = ( oldX + newX ) % x$$anonymous$$ax;


That code adds two numbers together and returns a number (less than x$$anonymous$$ax) that represents what the remainder would have been if you divided them.

In other words... If you're at x-value 100 and you add 3 to it, the result will be 3 (because 100 divided by 101 equals 1 with a RE$$anonymous$$AINDER OF 3).

Using the modulus operator makes it so array index values "wrap" when they get to the limit of the array.

You would use similar logic to "wrap" a rotation so it goes back to 1 (rather than 361 degrees) after it hits 360.

Hope this helps...

avatar image jahroy · Nov 13, 2011 at 10:14 PM 1
Share

That sounds like a syntax error.

$$anonymous$$y guess would be you're either using parentheses where you shouldn't or you're not using parenthesese where you should.

I think you see that in situations like this:

function name: booleanFunction

code:

 if ( booleanFunction ) {
   /* do stuff */
 }


In the above, there SHOULD be parentheses after booleanFunction, because it's a function.

avatar image syclamoth · Nov 13, 2011 at 11:55 PM 1
Share

I think I might be using 'Length' wrong. It's possible that I'm not referring to the one that is appropriate for multidimensional arrays. You'll have to look that one up, I'm afraid.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

For all elements above a certain index 1 Answer

For Loop Array Problem 1 Answer

How to deal with for loop and array? 0 Answers

For Loop Not Iterating Properly? 2 Answers

Adding an object to an array only if it is not in the array 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