Unity terrain generator script not working all of a sudden.

Hello, yesterday i was working on a unity project and it was working perfectly. I decided to call it a night, so i saved my project and went to bed. I woke up, loaded the project again, pressed run (without changing anything) and all of a sudden the project doesnt work. Here is what it looks like when i run it. Game objects show up but the mesh that i scripted will not appear for some reason.

To sum up what the script does is the Terrain Generator object containing the script TerrainGenerator

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

public class TerrainGenerator : MonoBehaviour
{
    public int _mapWidth, _mapheight;

    public static int _chunkWidth = 32;
    public static int _chunkHeight = 32;

    public enum LOD
    {
        High,
        Medium,
        Low,

    }

    public void Awake()
    {
        //for (int i = 0; i < 1; i++)
        //{
        MeshGenerator.GenerateChunk(Vector3.zero);

        MeshGenerator.GenerateMesh(0, "High");
        MeshGenerator.DrawMesh();
        //}
    }
   
}

This script calls the MeshGenerator Class to create a new game object, sets a mesh to the object, and then draw it onto the screen.

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

public class MeshGenerator
{
    public static Mesh mesh;
    private static Material _chunkMaterial;
    private static GameObject _chunk;

    public static void GenerateChunk(Vector3 position)
    {
        _chunkMaterial = Resources.Load("Materials/ChunkMaterial", typeof(Material)) as Material;
        _chunk = new GameObject("Chunk", typeof(MeshFilter), typeof(MeshRenderer));
        mesh = new Mesh();

        mesh = _chunk.GetComponent<MeshFilter>().mesh;
        _chunk.GetComponent<MeshRenderer>().material = _chunkMaterial;
       
        _chunk.AddComponent<Chunk>();
        _chunk.tag = "Terrain";
        _chunk.transform.localPosition = position;
       
    }

    public static void GenerateMesh(int Offset, string Lod )
    {
        string _lod = Lod;
        int _width = TerrainGenerator._chunkWidth;
        int _height = TerrainGenerator._chunkHeight;
        int _highLod = 4;
        int _mediumLod = 1;

        if (_lod == "Medium")
        {
            int chunkResolution = ((_width + 1) * _highLod) * ((_height + 1) * _highLod);
            Vector3[] vertices = new Vector3[chunkResolution];
            Vector2[] uvs = new Vector2[chunkResolution];

            for (int y = 0, i = 0; y < (_height * _highLod) + 1; y++)
            {
                for (int x = 0; x < (_width * _highLod) + 1; x++)
                {
                    vertices[i] = new Vector3(x * (1f / _highLod), 0, y * (1f / _highLod));
                    uvs[i] = vertices[i];
                    i++;
                }
            }

            //Draws Triangles to the vertices.
            int[] triangles = new int[(((_width + 1) * _highLod) * (_height + 1) * _highLod) * 6];
            for (int y = 0, vert = 0, tri = 0; y < (_height * _highLod); y++)
            {
                for (int x = 0; x < (_width * _highLod); x++)
                {
                    //This Works
                    triangles[tri + 0] = vert + 0;
                    triangles[tri + 1] = vert + ((_width * _highLod) + 1);
                    triangles[tri + 2] = vert + 1;
                    triangles[tri + 3] = vert + 1;
                    triangles[tri + 4] = (vert + (_width * _highLod)) + 1;
                    triangles[tri + 5] = (vert + (_width * _highLod)) + 2;
                    tri += 6;
                    vert++;
                }
                vert++;
            }
           
        }
       
        else if (_lod == "High")
        {
            int chunkResolution = ((_width + 1) * _mediumLod) * ((_height + 1) * _mediumLod);
            Vector3[] vertices = new Vector3[chunkResolution];
            Vector2[] uvs = new Vector2[chunkResolution];

            for (int y = 0, i = 0; y < (_height * _mediumLod) + 1; y++)
            {
                for (int x = 0; x < (_width * _mediumLod) + 1; x++)
                {
                    vertices[i] = new Vector3(x * (1f / _mediumLod), 0, y * (1f / _mediumLod));
                    uvs[i] = vertices[i];
                    i++;
                }
            }

            //Draws Triangles to the vertices.
            int[] triangles = new int[(((_width + 1) * _mediumLod) * (_height + 1) * _mediumLod) * 6];
            for (int y = 0, vert = 0, tri = 0; y < (_height * _mediumLod); y++)
            {
                for (int x = 0; x < (_width * _mediumLod); x++)
                {
                    //This Works
                    triangles[tri + 0] = vert + 0;
                    triangles[tri + 1] = vert + ((_width * _mediumLod) + 1);
                    triangles[tri + 2] = vert + 1;
                    triangles[tri + 3] = vert + 1;
                    triangles[tri + 4] = (vert + (_width * _mediumLod)) + 1;
                    triangles[tri + 5] = (vert + (_width * _mediumLod)) + 2;
                    tri += 6;
                    vert++;
                }
                vert++;
            }
           
        }

    }

    public static void UpdateChunkLocation(Vector3 chunkLocation)
    {
       
    }

    public static void DrawMesh()
    {
        mesh.Clear();
        mesh.vertices = _chunk.GetComponent<MeshFilter>().mesh.vertices;
        mesh.triangles = _chunk.GetComponent<MeshFilter>().mesh.triangles;
        mesh.uv = _chunk.GetComponent<MeshFilter>().mesh.uv;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
    }

    public static void DeleteMesh()
    {
        mesh.Clear();
    }
}

Each new Chunk object contains a script called Chunk which at the moment contains nothing at all.

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

public class Chunk : MonoBehaviour
{
    private bool isPlayerUnderThisChunk = false;

    private void Awake()
    {
       
    }

    private void Update()
    {
        //if player is on chunk.
    }

   
}

As i stated previously, yesterday this was working perfectly with no issues. And now i have no idea why it isnt working. I did not change anything, all i did was save it. Could anyone please give me some information as to what is going on here? Is something corrupted or am i just dumb and overlooked a simple setting? I checked the layers tab to see if i was hiding the objects but that is not the case.

Im spending hours trying to figure it out with no success. Just some background info, im pretty new to unity and C#. I only have about a months worth of experience with the software and language (constantly watching tutorials and videos trying to learn new things) so If my code looks awful please dont be too harsh as it is a work in progress. Also i am very new to this forum so if i make a mistake anywhere please point it out to me so i can learn from it. Thank you!

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

Beyond that, please consider using proper industrial-grade source control in order to guard and protect your hard-earned work.

Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

As far as configuring Unity to play nice with git, keep this in mind:

https://discussions.unity.com/t/736093/3

Here’s how I use git in one of my games, Jetpack Kurt:

https://discussions.unity.com/t/807568/3

Using fine-grained source control as you work to refine your engineering:

https://discussions.unity.com/t/826718/2

Share/Sharing source code between projects:

https://discussions.unity.com/t/719810/2

Setting up an appropriate .gitignore file for Unity3D:

https://discussions.unity.com/t/834885/5

Generally setting Unity up (includes above .gitignore concepts):

https://thoughtbot.com/blog/how-to-git-with-unity

It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place.

“Use source control or you will be really sad sooner or later.” - StarManta on the Unity3D forum boards

1 Like