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!