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 ThePixelatedSword · Aug 20, 2018 at 10:20 PM · proceduralcrashinggeneration

Unity Crashing when this function is called.

I have a button that runs this function.

 public void Generate()
     {
         laserPos = new Vector3(-terrainSize, 500, -terrainSize);
         Quaternion q = new Quaternion();
         q.eulerAngles = new Vector3(-90, 0);
         randTransform.rotation = q;
         int distanceToCover = 100 / densityPer100Meters;
         x = 0;
         int z = -terrainSize;
         i = 0;
         begin:
         while (x < terrainSize + 1)
         {
             Invoke("XGen", 5.1f);
             laserPos = new Vector3(x, 500, z);
         }
         if (x == terrainSize && z < terrainSize)
         {
             z += 1;
             x = 0;
             goto begin;
         }
         if (x == terrainSize && z == terrainSize)
         {
             Debug.Log("RGE is finished.");
         }
     
     }
 
     public void XGen ()
     {
         
         int distanceToCover = 100 / densityPer100Meters;
         RaycastHit hit;
         if (Physics.Raycast(laserPos, randTransform.forward, out hit, 500))
         {
             if (hit.transform.tag == "Terrain")
             {
                 int rx = Random.Range(-5, 5);
                 int rz = Random.Range(-5, 5);
                 Quaternion rq = new Quaternion();
                 Vector3 placeHolder = new Vector3(0, Random.Range(-179, 180), 0);
                 rq.eulerAngles = placeHolder;
                 Vector3 spawnPos = hit.point;
                 spawnPos.x += rx;
                 spawnPos.z += rz;
                 int rPrefab = Random.Range(0, spawnablePrefabs.Length + 1);
                 spawns[i] = Instantiate(spawnablePrefabs[rPrefab], spawnPos, rq);
 
 
                 x += distanceToCover;
                 
             }
             else
             {
                 x += distanceToCover;
             }
         }
         
     }

It's meant to spawn in a prefab from an array in a location on the map. When I click th UI button to call this function, Unity freezes and becomes unresponsive. Does anyone know the cause of this or how to fix it.

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

1 Reply

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

Answer by JVene · Aug 20, 2018 at 11:48 PM

This snippet:

      begin:
      while (x < terrainSize + 1)
      {
          Invoke("XGen", 5.1f);
          laserPos = new Vector3(x, 500, z);
      }
      if (x == terrainSize && z < terrainSize)
      {
          z += 1;
          x = 0;
          goto begin;
      }

Suggests an infinite loop. What would change x so that it is < terrainSize + 1? XGen? Where in XGen is that certain to happen?


If if that does happen, does it fire the next if? You've used a goto begin; there which could define a secondary infinite loop. These are the most likely places in which such a loop would completely lock Unity at runtime.


There is a long running philosophy about how 'goto' is always a bad idea, and as a veteran of 30 years in programming in a number of languages, I have to agree. 'goto' can cause all manner of issues without any good need. Every bracket, every loop is an implied goto, which makes the keyword 'goto' unnecessary. What you have is more logical to fashion as nested while loops, or perhaps a do while with a nested while.


From what I read, the begin label is, in reality, what the code should be doing while x == terrainSize && z < terrainSize, and while I understand you reason for using Invoke, I suspect the performance costs are too high to be a good solution here. It may be better to prepare data to forward to Update for calling Instantiate on a completed set of data. This version makes the (I assume threaded) Generate function waste it's potential waiting on the Invoke.

Comment
Add comment · Show 3 · 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 ThePixelatedSword · Aug 21, 2018 at 12:29 AM 0
Share

I have tried running the script with the goto set as a comment and it still crashed and originally the invoke was a direct call but I changed to an invoke to see if spacing out the calls by a couple of seconds would help. Both of these still crash unity.

avatar image JVene ThePixelatedSword · Aug 21, 2018 at 01:56 AM 0
Share

At the risk of being rude, did you read my question: "What would change x so that it is < terrainSize + 1? XGen? Where in XGen is that certain to happen?".

This isn't about how the loop(s) are organized so much as how they ter$$anonymous$$ate.

avatar image ThePixelatedSword JVene · Aug 22, 2018 at 09:23 PM 1
Share

I see what you mean now sorry I had thought I had put a x++; in the Xgen function but now that I look I didn't thank you!

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

94 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

Related Questions

Simple 2D procedural galaxy map 2 Answers

Procedural Generation Instantiating GameObjects 1 Answer

Procedural Cylinder Generation 2 Answers

OnValidate changed fields doesnt save after exit the play mode. 1 Answer

3d Perlin Noise? 3 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