• 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 CrazyCyfro · May 13, 2014 at 01:04 PM · javascriptarrays

Array returning wrong values?

 #pragma strict
 
 var hexPrefab: GameObject;
 private var worldWidth: float;
 private var worldHeight: float;
 private var spawnSpeed: float;
 private var letters = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D');
 private var numbers = new Array('1','3','5','7','9','11','13','15','17','19','21','23','25','27','29');
 
 function Start () {
     worldWidth = 3.89*30;
     worldHeight = 7.48*30*0.5;
     spawnSpeed = 0.01;
 
     CreateHex();
 
 }
 
 function Update () {
 
 }
 
 function CreateHex() {
     yield WaitForSeconds(spawnSpeed);
     for (var x: float = 0; x < worldWidth; x+=3.89) {
         yield WaitForSeconds(spawnSpeed);
         for (var y: float = 0; y < worldHeight; y += 7.48) {
             yield WaitForSeconds(spawnSpeed);
             var hex = Instantiate(hexPrefab);
             hex.name = letters[x/3.89].ToString() + numbers[y/7.48].ToString(); //naming
             hex.transform.position = new Vector3(this.transform.position.x + x, this.transform.position.y + y, this.transform.position.z);
         }
     }
 }

Hello, I'm having a problem with using arrays to return values. In this code I'm trying to spawn a field of hexagons with 2 separate spawners while naming every individual hex that has spawned. Thus both would have this same code, except one would have even numbered rows and be positioned differently. The rows and columns of the hexes are like chess, with the bottom row being row 1 and the leftmost column being column a. Never mind the actual spawning though.

Problem is when naming the hexes (eg a1, f7), even though I've tested explicitly the values for both x and y, at certain instances the array still returns me the wrong value. For example, at the fourth column (x = 11.67), letters[x/3.89] should return 'd', but it still returns 'c' to me. I've checked countless times and the value of x and y are what they are supposed to be when naming the hex, but the array is always returning the wrong value. This also happens to the numbers in the name as well. Is it some obscure coding mistake I'm making or a bug on Unity's side? I'm still relatively new so pardon me.

Thanks.

Comment
Add comment · Show 3
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 Nerevar · May 13, 2014 at 01:43 PM 0
Share

round will work :p

I tried it

avatar image Syndias · May 13, 2014 at 01:48 PM 0
Share

Try using $$anonymous$$athf.Round(x/3.89), ins$$anonymous$$d of implicitly casting to int

avatar image CrazyCyfro · May 14, 2014 at 04:57 AM 0
Share

Thanks everybody, it worked! Love you guys

1 Reply

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

Answer by $$anonymous$$ · May 13, 2014 at 01:08 PM

Try to round to int.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Never repeat last random audioclip 2 Answers

Having Multiple Controllable FPS Units Selected From A Singular RTS Mode? 0 Answers

Javascript 2D arrays: null values and slicing 1 Answer

Converting JS Array to Generic List 2 Answers

2D array error object does not support slicing 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