Unity Crach. Grid Visual

Hello! when try to run this code: My computer crash!

Wath can I do?

public class GridSystemVisualSIngle : MonoBehaviour
{
[SerializeField] Transform gridVisualSystemSIngle;

private void Start()
{
    for (int x = 0; x < LevelGrid.Instance.GetWidth(); x++)
    {
        for (int z = 0; z < LevelGrid.Instance.GetHeigth(); z++)
        {
            GridPosition gridPosition = new GridPosition(x, z);
            Instantiate(gridVisualSystemSIngle, LevelGrid.Instance.GetWorldPosition(gridPosition), Quaternion.identity);
        }
    }
}

}

Everything appears correct, are you sure the issue is in that code? If you comment that out it no longer happens?
Does it crash your entire computer? Not just Unity?
How big is your grid? If it’s insanely big you might be running out of memory.
Are you somehow modifying the grid height?
Check the crash logs to see what they say Code Monkey - Unity Tip: Unity Crashed! Why?

This code is in GridSystemVisualSingle which it shouldn’t be. You are running this over and over and over again because you instantiate another GridSystemVisualSingle which runs it again, doing the same thing. Unity crashes because you are creating a gazillion GridSystemVisualSingle objects

4 Likes

Oh right good catch I didn’t notice that, yea that script is running on every Single object which in turn creates more objects which in turn run that script and create more and so on

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms