0

So, I have a terrain and I'm trying to add little batches of flowers all over my terrain I wrote this code but when I launch my game it loads to infinity can you help me? This is the part of my code that instantiate the flowers when I remove it the game launch.

    Vector2[] FlowerPatch = { };
    bool done = false;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        animator = GetComponent<Animator>();

        TerrainData terrainData = Terrain.activeTerrain.terrainData;

        for (int i = 0; i < 1; i++)
        {
            int nFlowers = Random.Range(1, 5);
            Vector2 patchPos = new Vector2(0, 0);
            Debug.Log("Not Done");
            while (!done)
            {
                int x = Random.Range(0, 1000);
                int y = Random.Range(0, 1000);
                Vector2 pos = new Vector2(x, y);
                Debug.Log("Searching Position");
                for (int i1 = 0; i1 < FlowerPatch.Length; i1++)
                {
                    if (Mathf.Sqrt(Mathf.Pow(pos.x - FlowerPatch[i1].x, 2) + Mathf.Pow(pos.y - FlowerPatch[i1].y, 2)) > 2)
                    {
                        done = true;
                        patchPos = pos;
                        FlowerPatch.Append(pos);
                    }
                    else done = false;
                }
            }
            for (int j = 0; j < nFlowers; j++)
            {
                Vector3 FlowerPos = new Vector3(patchPos.x + Random.Range(-1.0f, 1.0f), 0, patchPos.y + Random.Range(-1.0f, 1.0f));
                FlowerPos.y = terrainData.GetHeight((int)FlowerPos.x, (int)FlowerPos.z);
                Instantiate(flower, FlowerPos, Quaternion.identity);
                Debug.Log("Instantiated Flower");
            }
        }

    }

I would appreciate any advice on ow to solve my problem :)

I tried moving my code to the update function lowering the number of batches I want but nothing works

EDIT: tried some things as you suggested but it still doesn't work

for (int i = 0; i < 2500; i++)
       {
           int nFlowers = Random.Range(1, 5);
           Vector2 patchPos = new Vector2(0, 0);
           Debug.Log("Not Done");
           while (!done)
           {
               int x = Random.Range(0, 1000);
               int y = Random.Range(0, 1000);
               Vector2 pos = new Vector2(x, y);
               Debug.Log("Searching Position");
               for (int i1 = 0; i1 < FlowerPatch.Length; i1++)
               {
                   if (Mathf.Sqrt(Mathf.Pow(pos.x - FlowerPatch[i1].x, 2) + Mathf.Pow(pos.y - FlowerPatch[i1].y, 2)) > 2)
                   {
                       done = true;
                       patchPos = pos;
                       break;
                   }
                   else done = false;
               }
           }
           for (int j = 0; j < nFlowers; j++)
           {
               Vector3 FlowerPos = new Vector3(patchPos.x + Random.Range(-1.0f, 1.0f), 0, patchPos.y + Random.Range(-1.0f, 1.0f));
               FlowerPatch.Append(patchPos);
               FlowerPos.y = terrainData.GetHeight((int)FlowerPos.x, (int)FlowerPos.z);
               Instantiate(flower, FlowerPos, Quaternion.identity);
               Debug.Log("Instantiated Flower");
           }
       }
  • There could be a lot of reasons. Such as this code is on the flowers so you get multiple copies but my guess is if you put some debugs in done never breaks that loop because the for loop inside iterates an exact number of times and if the done and end of that for loop dont coincide its reset to false again next time on the for loop because you dont break out – BugFinder Aug 31 '23 at 13:52
  • @BugFinder, I don't understand I'm sorry can you tell me more precisely how I can resolve my problem? And thanks for your quick response ;) – kayardurdass Aug 31 '23 at 14:09
  • I feel like that `Append` might be the culprit. You run the loop for the length of the array, but during every successful "range find" (cannot tell the exact purpose) you add a new element to the array. In my mind that would mean that the for loop runs for ever. – Voidsay Aug 31 '23 at 14:12
  • @kayardurdass as im on my phone. Look up the word break. It leaps out of that loop (so in this case the for loop) at the point its true. So if you had 12000 names in a list and you found it on the first entry you dont need to check the 11999 others. :) – BugFinder Aug 31 '23 at 16:13

0 Answers0