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 Bioxent · Nov 25, 2014 at 08:25 PM · transformpositionloopgridgenerate

Change transform.position.y in a loop

I am trying make a game that will generate a grid of blocks in start. This code makes them generate on x line, but when I try to add y in a loop and set it in the Vector3, it doesn't seem to work. *The problem is that it doesn't switch to 2nd line in y. using UnityEngine; using System.Collections;

 public class GenerateTileyyy: MonoBehaviour {
     
     public GameObject tile;
     public float distanceBetweenTiles = 1.5f;
     public float spawnSpeed = 0;
     public float grid = 15;
     public float x = 0;
     public float y = 0;
     
     // Use this for initialization
     void Start () {
 
                 float x = transform.position.x;
                 float y = transform.position.y;
 
                 for (x=0; x < grid; x=x+1.5f) {
 
                         Instantiate (tile, new Vector3 (x, y, 0), Quaternion.identity);
                         if (x == 10f) {
                                 x = 0;
                                 y = y + 2;
                         }
                 }
         }
 }
 
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 Bioxent · Nov 25, 2014 at 08:42 PM 0
Share

It doesn't switch to 2nd line in the game screen. Just generates only 1st line/row.

avatar image FrostyNomad · Nov 25, 2014 at 08:52 PM 0
Share

You're declaring x (and also y) multiple times. Try using different variable names for each of them.

1 Reply

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

Answer by Uldeim · Nov 25, 2014 at 09:37 PM

You're declaring x and y twice, which is silly, but irrelevant.

Your main problem is that 1.5 * 6 is 9, and 1.5 * 7 is 10.5, so you're never going to get x == 10f. Actually, because float arithmetic is imprecise, it's a bad idea to test equality anyways (though 0.5f is actually exact, so it's not a big deal here).

Change your if to x > 10f and you should be in good shape... or you would be, except it will actually cause an infinite loop, since x will then always be less than grid. While you could change the second argument of your for loop to y < grid, it's weird and unnatural to have different variables in there.

I suggest instead that you use a slightly more popular pattern: Nested loops.

  for (float x = 0; x < grid; x += 1.5f)
  {
    for (float y = 0; y < grid; y += 2f)
    {
       Instantiate(tile, new Vector3(x,y,0), Quaternion.identity);
    }
  }
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 FrostyNomad · Nov 26, 2014 at 12:17 AM 0
Share

This is a good answer, just a couple of quibbles: 1. your answer will draw the tiles Bottom->Top then Left->Right versus the way the OP was trying to draw them. 2. While it wasn't a factor here, declaring variables with the same name is a really bad practice that can lead to tremendous headaches down the road.

avatar image Uldeim · Nov 26, 2014 at 12:40 AM 0
Share

@FrostyNomad - Agreed on both points. To draw horz/vert ins$$anonymous$$d, just switch the order of the for loops.

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

transform.position error 1 Answer

Why is my variable updating when it shouldn't? 1 Answer

Getting an Objects position once 2 Answers

How do I make one transform rotation the same as anothers? 2 Answers

Objects with same position appear in different places 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