Unity is crashing when entering Gamemode

Hey guys,

has anyone watched the tutorial-series, made by Sebastina Langue, about procedural terrain generation
Link: https://www.youtube.com/playlist?list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3.
Im stuck on “Episode 07 | Endless Terrain”, because everytime I want to start the game Unity crashes.
I have checked everything in the scipt and Monodevelop doesn´t shows me any errors.

I hope you can give me some tipps what the problem might be.

Thanks :slight_smile:

Have you checked the crash logs? Unity - Manual: Log files

I think I have expressed my self wrong ^^. The Unity Editor is not crashing, I had to close it with the taskmanager beacuse it wasn´t responsing anymore :slight_smile:

Perhaps you’ve accidently made a mistake and have an infinite loop somewhere in your code?
Try to undo what you’ve done recently until it works again.

Without code noone will be able to help you though and I bet noone’s gonna watch the series just for this reason.

This is the code wich is making problems →

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

public class EndlessTerrain : MonoBehaviour {
    public const float maxViewDst = 450;
    public Transform viewer;

    public static Vector2 viewerPosition;
    int chunkSize;
    int chunksVisibleInViewDst;

    Dictionary<Vector2, TerrainChunk> terrainChunkDict = new Dictionary<Vector2, TerrainChunk>();
    List<TerrainChunk> terainChunkLastUpdate = new List<TerrainChunk>();

    void Start(){
   
        chunkSize = MapGenerator.mapChunkSize - 1;
        chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / chunkSize);
   
    }

    void Update(){
   
        viewerPosition = new Vector2 (viewer.position.x, viewer.position.y);
        UpdateVisibleChunks ();
   
    }

    void UpdateVisibleChunks() {

        for (int i = 0; i < terrainChunksVisibleLastUpdate.Count; i++) {
            terrainChunksVisibleLastUpdate [i].SetVisible (false);
        }
        terrainChunksVisibleLastUpdate.Clear ();

        int currentChunkCoordX = Mathf.RoundToInt (viewerPosition.x / chunkSize);
        int currentChunkCoordY = Mathf.RoundToInt (viewerPosition.y / chunkSize);

        for (int yOffset = -chunksVisibleInViewDst; yOffset <= chunksVisibleInViewDst; yOffset++) {
            for (int xOffset = -chunksVisibleInViewDst; xOffset <= chunksVisibleInViewDst; xOffset++) {
                Vector2 viewedChunkCoord = new Vector2 (currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);

                if (terrainChunkDictionary.ContainsKey (viewedChunkCoord)) {
                    terrainChunkDictionary [viewedChunkCoord].UpdateTerrainChunk ();
                    if (terrainChunkDictionary [viewedChunkCoord].IsVisible ()) {
                        terrainChunksVisibleLastUpdate.Add (terrainChunkDictionary [viewedChunkCoord]);
                    }
                } else {
                    terrainChunkDictionary.Add (viewedChunkCoord, new TerrainChunk (viewedChunkCoord, chunkSize, transform));
                }

            }
        }
    }

    public class TerrainChunk{

        GameObject meshObject;
        Vector2 position;
        Bounds bounds;

        public TerrainChunk(Vector2 coord, int size, Transform parent){
            position = coord * size;
            bounds = new Bounds(position, Vector2.one * size);

            Vector3 positionV3 = new Vector3(position.x,0,position.y);

            meshObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
            meshObject.transform.position = positionV3;
            meshObject.transform.localScale = Vector3.one * size/10;
            meshObject.transform.parent = parent;
            SetVisible(false);
        }

        public void UpdateTerrainChunk(){
       
            float viewerDstFromNearesEdge = Mathf.Sqrt(bounds.SqrDistance (viewerPosition));
            bool visible = viewerDstFromNearesEdge <= maxViewDst;
            SetVisible (visible);
        }

        public void SetVisible(bool visible){
       
            meshObject.SetActive (visible);
       
        }

        public bool isVisible(){
            return meshObject.activeSelf;
        }
    }
}

Code works, what’s your chunk size? You should probably increase it. The lower it is, the more chunks will be created, e.g. size 50 => 361, 5 => 32761, 4 => 50625

Besides that, there are some redundant lookups in the dictionaries. And some other things may cause problems during runtime, e.g. all the chunks are kept alive. Not sure if the author addresses these issues but the current state can bring your system to its limit sooner or later.

1 Like

Thanks for your help guys:) It´s working now ^^