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 Vioswift · Jul 20, 2014 at 12:39 PM · loopblock

Unity 2D C# - Creating a Box Out Of Boxes

This is my half finished code

Right now it does 1 line and that line is 10 boxes the 2nd line just goes on until it reaches the max

so it kinda looks like this at the moment

++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++

and its meant to be a 10 by 10 square ++++++++++ ++++++++++ ++++++++++

and so on

can anyone help me do this, I think I am meant to do some sort of loop with the height

     //start of spawning location
     Vector3 StartPosition = transform.position;

             int BlockWidth = 10;
             int BlockHeight = 10;
             int BlockAdder = 0;
     while (BlockAdder < (BlockWidth * BlockHeight)) {
         if (BlockAdder == BlockWidth) {

             //GroundLayerArrayPositive.Add(1); //Block Type
             //GroundLayerArrayPositive.Add(BlockWidth); //X Location
             //GroundLayerArrayPositive.Add(BlockHeight); // Y Location

             Instantiate(DirtBlock, transform.position = new Vector3(StartPosition.x + 0.5f, StartPosition.y - 0.5f, 0), transform.rotation);


             BlockHeight++;
         }
         else
         {

             Instantiate(DirtBlock, transform.position = new Vector3(transform.position.x + 0.5f, transform.position.y, 0), transform.rotation);
         }
         BlockAdder++;
                     }
Comment
Add comment · Show 4
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 SirCrazyNugget · Jul 20, 2014 at 10:26 AM 2
Share

If you know the required number of iterations you'd be better off using a for loop, for creating a square use a second nested for loop (or while loop if you prefer).

Your Instantiate statement is somewhat curious too, your second parameter:

 transform.position = new Vector3(StartPosition.x + 0.5f, StartPosition.y - 0.5f, 0);

This will create a new Vector3, set the transform.position of the object which has the script attached to it and also set it to the Instantiated DirtBlock. It may be what you're after but I doubt it.

 for(int x = 0; x < BlockWidth; x++){
   for(int y = 0; y < BlockHeight; y++){
     Instantiate (DirtBlock, new Vector3(transform.position.x + (x * 0.5f), transform.position.y + (y * 0.5f), 0);
   }
 }


avatar image Vioswift · Jul 21, 2014 at 08:33 AM 0
Share

I used your code, I had to modify it so it wouldn't have an error and i got this

 Instantiate(DirtBlock, transform.position = new Vector3(transform.position.x + (x * 0.5f), transform.position.y + (y * 0.5f), 0), transform.rotation);

And it does this weird wave pattern like an inside out cloud.

avatar image NoseKills · Jul 21, 2014 at 03:58 PM 0
Share

Why do you have an "assignment to a variable" as the second parameter of the Instantiate call?

$$anonymous$$aybe assignments can have a return value in C# but in most languages you would get at least a warning from that :P

Depending on what a syntax like that does, it could cause some weirdness as you are moving the reference object every time and then positioning the New object based on the other, moved object.

Try with just

 Instantiate (DirtBlock, new Vector3(...

Ins$$anonymous$$d of

 Instantiate(DirtBlock, transform.position = new Vector3(...

CrazyNugget's suggestion should work ok. :)

avatar image Vioswift · Jul 22, 2014 at 07:58 AM 0
Share

O$$anonymous$$G!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! It worked!!!! Thank you so sos sosososoosososososos much!!!!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Stop animation in Animation Controller 0 Answers

Guarantee loop order of child objects. 1 Answer

I dont know why im getting a null reference exception 1 Answer

How can I move back to Element 0 in an array to reset a loop? 1 Answer

AoTTG - Modding with Visual Basics + .NetReflector SFX Mod 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