0
\$\begingroup\$

So I have this code that has four for-loops inside each other, so there are a lot of actions, but no new Space should be needed. Still, when trying to run this, Unity stops responding and in the Task-Manager I just see the RAM-Usage steadily climbing up while the CPU-Usage is pretty much constantly on 10%.

This is the Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneGenerator : MonoBehaviour
{

    public GameObject spawnee;

    // Start is called before the first frame update
    void Start()
    {
        pos4d mpos = new pos4d();
        float r = 1;

        pos4d campos = new pos4d();
        campos.s = 3;

        pos4d viewpos = new pos4d();
        viewpos.s = 3.5f;

        //pos4d[,,] grid = new pos4d[10,10,10];
        pos4d dir = new pos4d();
        pos4d raypoint = new pos4d();

        for (int i = 0;i < 10;i++) {
            for (int j = 0;j < 10;j++) {
                for (int k = 0;k < 10;k++) {
                    /*
                    grid[i,j,k] = new pos4d();
                    grid[i,j,k].x = i/10-0.5f;
                    grid[i,j,k].y = j/10-0.5f;
                    grid[i,j,k].z = k/10-0.5f;
                    grid[i,j,k].s = 3;
                    */

                    dir.x = (i/10-0.5f - viewpos.x)/10;
                    dir.y = (j/10-0.5f - viewpos.y)/10;
                    dir.z = (k/10-0.5f - viewpos.z)/10;
                    dir.s = (3 - viewpos.s)/10;

                    raypoint.x = i/10-0.5f;
                    raypoint.y = j/10-0.5f;
                    raypoint.z = k/10-0.5f;
                    raypoint.s = 3;

                    for (int step = 0;step < 1000;step++) {
                        Debug.Log("" + (i+j+k+step));
                        if (Mathf.Sqrt(Mathf.Pow(raypoint.x - mpos.x,2) + Mathf.Pow(raypoint.y - mpos.y,2) + Mathf.Pow(raypoint.z - mpos.z,2) + Mathf.Pow(raypoint.s - mpos.s,2)) <= r) {
                            Instantiate(spawnee,new Vector3(i/10-0.5f,j/10-0.5f,k/10-0.5f),Quaternion.identity);
                            break;
                        }
                        raypoint.x += dir.x;
                        raypoint.x += dir.y;
                        raypoint.x += dir.z;
                        raypoint.x += dir.s;
                    }
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

public class pos4d {
    public float x = 0;
    public float y = 0;
    public float z = 0;
    public float s = 0;
}
```
\$\endgroup\$
2
\$\begingroup\$

Instantiate seems to be the culprit here. You're trying to instantiate THOUSANDS of new game objects in a single frame. I would recommend to delay the instantiate by batches of 100 per frame.


EDIT:

And I'm not even mentionning the thousands of new pos4d and new Vector3 that are called.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Well i am doing it in the Start() Function tho ? should I still just put in delays ? \$\endgroup\$ – samurzl Apr 21 at 10:01
  • 1
    \$\begingroup\$ What does the spawnee object do? Because you'll get a pike after the Start method ends due to instantiate and after that all the Start of all the spawnee will be called and then the update and all. But I don't teach you anything here. Also did you have a look to the profiler? \$\endgroup\$ – lvictorino Apr 21 at 10:04
  • \$\begingroup\$ @Ivictorino The spawnee doesnt have any code inside. What is the profiler ? \$\endgroup\$ – samurzl Apr 21 at 10:07
  • 1
    \$\begingroup\$ @samurzl The spawnee might not have any code that you wrote, but what about all the standard Unity components on it? By the way, if you intend to make a game which has a very large number of relatively lightweight objects, then you might want to look at the new Data-Oriented Tech Stack. It's optimized for scenarios like that. \$\endgroup\$ – Philipp Apr 21 at 11:19
  • 1
    \$\begingroup\$ Worst case you are trying to instantiate 1.000.000 objects, even if you place those in batches of 100, it will crash. While I could not find how much Unity actually can handle, first few google hits are suggestion in the range of thousands. Another small unnecessary thing to do seems the condition, you are doing basically 1.000 times the same calculation and sqrt/ pow could add a bit as well. Depending what you want to do, there should be a different solution for using less objects. \$\endgroup\$ – Zibelas Apr 21 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.