Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do I prevent a infinite loop in my procedural generation

I am trying to take pockets of Perlin noise, and generate a dungeon inside of it, but to keep it to one pocket, I tried to make a self-duplicaating object that would spread thoughout and die out, without going back on itself. It would generate a sphere where it was that would delete any new objects. exept it doesn't work. I get an infinite loop that freezes up Unity and breaks.

Here is my code:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine

  4. public class RoomGen : MonoBehaviour
  5. {
  6. public float RoomValue;
  7. public float seed = 1.457582f;
  8. public GameObject Marker;
  9. public GameObject Self;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. }
  14. void LateUpdate()
  15. {
  16. RoomValue = Mathf.PerlinNoise(transform.position.x * seed, transform.position.z * seed);
  17. if (RoomValue >= .4f)
  18. {
  19. Instantiate(Marker, transform.position, transform.rotation);
  20. Instantiate(Self, transform.position + new Vector3(-1, 0 ,0), transform.rotation);
  21. Instantiate(Self, transform.position + new Vector3(1, 0, 0), transform.rotation);
  22. Instantiate(Self, transform.position + new Vector3(0, 0, -1), transform.rotation);
  23. Instantiate(Self, transform.position + new Vector3(0, 0, 1), transform.rotation);
  24. Destroy(this.gameObject);
  25. }
  26. else
  27. {
  28. Destroy(this.gameObject);
  29. }
  30. }

  31. private void OnCollisionStay(Collision collision)
  32. {
  33. Destroy(this.gameObject);
  34. }
  35. }

In theory the collison should trigger before the late update, then destroying the object before it can spread. In practice it all breaks down into an infinite loop. Any advice would be appreaciated


  • SanderSander Member

    It might be an idea to run it in a coroutine with a delay on it. That way, you can more easily see what is causing the break.

  • @Sander I know where the problem is, but not how to fix it. The OnCollisionStay() is not triggering before the LateUpdate(), causing an inifnite loop

Sign In or Register to comment.