Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ItsKhanny · Aug 25, 2018 at 04:28 AM · for loopcs

Problem with creating randomized grid

I'm making a 2D grid with randomized sprites. But for some reason, my script will instantiate more GameObjects within a single for loop than is possible. I'm stuck with this problem and cannot solve it.

Here is some of my code

 private void CreateGrid()
     {
 
         print("A");
 
         for (int i = 0; i <= maxX; i++)
         {
 
             for (int k = 0; k <= maxY; k++)
             {
 
                 if (i != maxX && k != 0)
                 {
                     int air = Random.Range(0, 100);
 
                     if (air <= 50)
                     {
                         int block = Random.Range(0, 100);
 
                         GameObject go = null;
 
                         if (block < 65)
                         {
                             go = blockTypes[1];
                         }
                         else if (block >= 65 && block <= 70)
                         {
                             go = blockTypes[0];
                         }
                         else if (block > 70 && block <= 80)
                         {
                             go = blockTypes[3];
                         }
                         else if (block > 80 && block <= 98)
                         {
                             go = blockTypes[2];
                         }
                         else if (block > 98)
                         {
                             go = blockTypes[4];
                         }
 
                         GameObject g = Instantiate(go, Vector3.zero, Quaternion.identity);
                         g.name = g.name.Replace("(Clone)", "");
                         g.transform.position = startPos + new Vector3(i * blockSize, k * blockSize, 0);
                         g.transform.localScale = Vector3.one * blockSize;
                         blocks.Add(g);
                         blockPos.Add(g.transform.position);
                     }
                 }
 
             }
 
         }
 
     }

 public void ResetGrid()
     {
         if (!regeningGrid)
         {
             regeningGrid = true;
             for (int i = 0; i < blockPos.Count; i++)
             {
                 GameObject g = blocks[i];
                 blocks.RemoveAt(i);
                 blockPos.RemoveAt(i);
                 Destroy(g);
             }
             print("C");
             CreateGrid();
             game.IncLevel();
             regeningGrid = false;
         }
 
     }



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 Em3rgency · Aug 25, 2018 at 07:27 AM 0
Share

What do you mean by my script will instantiate more GameObjects within a single for loop than is possible? You haven't explained what your problem is. Also, how big are maxX and maxY?

avatar image Stratosome · Aug 25, 2018 at 07:27 AM 0
Share

Hey, since I don't quite have your variables and such to mess around with the script, could you describe exactly what the output of your CreateGrid function is?

avatar image eses · Aug 25, 2018 at 09:37 AM 0
Share

Hi @Its$$anonymous$$hanny - I'd first try the loop in simple form something like:

     private void CreateGrid()
     {
         for (int y = 0; y < maxY; y++)
         {
             for (int x = 0; x < maxX; x++)
             {
                 Debug.Log("x:" + x + " y:" + y);
             }
         }
     }


Then see if you still have problems;

For example your line: "if (i != maxX && k != 0)" only makes it so, that x can't be it's max value, so if you have 4, tile 3 in x-axis will be the last one.

Also, y-axis tile won't be created, when k is zero. So basically this is just convoluted way of removing rows and columns I think.

Also lines: "int air = Random.Range(0, 100);" and "if (air
But like @Em3rgency - said - you don't actually explain, what the problem is.

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

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

Related Questions

can mono touch be used to make unity iPhone plugins 0 Answers

how to select which object i want to use in itween C# 0 Answers

Instantiate for loop problem 2 Answers

Method to loop through an unknown number of variable combinations in C# 1 Answer

silly for loop problem 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