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 /
  • Help Room /
avatar image
0
Question by EpicCrownKing · Jan 20, 2020 at 06:54 PM · 2dloopgenerationtopdown

Weird infinite loop-or just long loading time

Hello, this going to be a bit of a hefty ask. I'm working on topdown terrain generation, and because I'm stubborn, I wanted to make my own system without using colliders. I've debugged the 141-line long script down to 1 buggy foreach loop. It seems to be running an infinite loop, but it could just take a long time to run. Here's the loop:

 foreach(GameObject spawnpoint in spawnpoints)
         {
             if(spawnpoint != null)
             {
                 GameObject tempRoom = Instantiate(room, spawnpoint.transform.position, spawnpoint.transform.rotation);
                 tempRoom.GetComponent<OutdoorRoom>().chains = chains + 1;
                 tempRoom.transform.parent = spawnpoint.transform.parent.transform.parent;
                 
                 if(System.Array.IndexOf(spawnpoints, spawnpoint) == 0)
                 {
                     Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[1]);
                     tempRoom.GetComponent<OutdoorRoom>().exits += "S";
                 }
                 if (System.Array.IndexOf(spawnpoints, spawnpoint) == 1)
                 {
                     Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[0]);
                     tempRoom.GetComponent<OutdoorRoom>().exits += "N";
                 }
                 if (System.Array.IndexOf(spawnpoints, spawnpoint) == 2)
                 {
                     Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[3]);
                     tempRoom.GetComponent<OutdoorRoom>().exits += "W";
                 }
                 if (System.Array.IndexOf(spawnpoints, spawnpoint) == 3)
                 {
                     Destroy(tempRoom.GetComponent<OutdoorRoom>().spawnpoints[2]);
                     tempRoom.GetComponent<OutdoorRoom>().exits += "E";
                 }
             }
         }


If I need to provide more code, I'm happy to. I'll be constantly working to fix the bug in the meantime. Thanks for everything, EpicCrownKing

Oh and Task Manager doesn't deem Unity as not responding, it just takes up about 80% of my PC. CORRECTION /\ Yes, it does say that it isn't responding. Whoops!

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

2 Replies

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

Answer by Hellium · Jan 20, 2020 at 07:36 PM

I don't know if the following will fix your issue, but it will improve the performances a lot

 for(int index = 0 ; index < spawnpoints.Length ; ++index)
 {
     GameObject spawnpoint = spawnpoints[index];

     if(spawnpoint == null) continue;
 
     OutdoorRoom tempRoom = Instantiate(room, spawnpoint.transform.position, spawnpoint.transform.rotation).GetComponent<OutdoorRoom>();
     tempRoom.transform.parent = spawnpoint.transform.parent.transform.parent;
     
     switch(index)
     {
         case 0:
             Destroy(tempRoom.spawnpoints[1]);
             tempRoom.exits += "S";
             break;
         case 1:
             Destroy(tempRoom.spawnpoints[0]);
             tempRoom.exits += "N";
             break;
         case 2:
             Destroy(tempRoom.spawnpoints[3]);
             tempRoom.exits += "W";
             break;
         case 3:
             Destroy(tempRoom.spawnpoints[2]);
             tempRoom.exits += "E";
             break;
     }
 }
Comment
Add comment · Show 4 · 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 EpicCrownKing · Jan 20, 2020 at 07:54 PM 0
Share

Never considered a switch statement. Whoops! That'll be really helpful. I'll plug that in and see if it works. It's almost like you know my code better than I do(which probably is the case)! Thanks a lot!

avatar image EpicCrownKing EpicCrownKing · Jan 20, 2020 at 08:04 PM 0
Share

It still lags out, I think there's an infinite loop. I'll work within my old code, make it work, then translate it into your optimized code. I'll certainly credit you in my code.

avatar image Hellium EpicCrownKing · Jan 20, 2020 at 08:10 PM 0
Share

Consider using the profiler to know where does the problem come from:

  • https://docs.unity3d.com/$$anonymous$$anual/Profiler.html

  • https://www.youtube.com/watch?v=sBpXiJ9G3OY

  • https://www.youtube.com/watch?v=1e5WY2qf600

  • https://www.youtube.com/watch?v=OSlOwJP8Z14

Show more comments
avatar image
0

Answer by EpicCrownKing · Jan 20, 2020 at 10:26 PM

I was being dumb and looked at the wrong piece of code. Thanks for the help @Hellium. The optimized code sped it up even more.

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.

Follow this Question

Answers Answers and Comments

242 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 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 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

Generate 2d star background 0 Answers

How do I get an animation to loop back from a certain point? 1 Answer

2D topdown projectiles movement 0 Answers

Please help me make GameObject FlipY when RotationZ is less than -90. I know something is wrong with my script I just need to help to fix it. 1 Answer

Why is my player and camera only moving in the scene view? 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