So There a ray that shoots from the object with the first script on this post to an empty gameobject assigned to the target variable. If the ray hits a square/block, it should destroy it. I get an error when the ray hits a block
The error:
MissingFieldException: System.Byte[,].
(…)
Assets/Resources/KeenansCoolResources/KeenansCoolScripts/VoxelGeneration/PolyDestroyer.js:31)
Code that the error is pointing to:
//Inspector Variables-----------------------------------------------------
var terrain : GameObject;
var target : GameObject;
//Hidden Variables--------------------------------------------------------
private var tScript;
private var layerMask : LayerMask = (1 << 0);
function Start ()
{
tScript = terrain.GetComponent("2DGeneratorScript");
}
function Update ()
{
var hit : RaycastHit;
var distance : float = Vector3.Distance(this.transform.position, target.transform.position);
if(Physics.Raycast(this.transform.position, (target.transform.position - this.transform.position).normalized, hit, distance, layerMask))
{
Debug.DrawLine(this.transform.position, hit.point, Color.green);
var point : Vector2 = new Vector2(hit.point.x, hit.point.y);
point += (new Vector2(hit.normal.x, hit.normal.y)) * -0.5;
tScript.blocks[Mathf.RoundToInt(point.x -0.5), Mathf.RoundToInt(point.y + 0.5)] = 0;
tScript.update = true;
}
else
{
Debug.DrawLine(this.transform.position, target.transform.position, Color.red);
}
}
And heres that terrain script:
import System.Collections.Generic;
//Inspector Variables-----------------------------------------------------
var xAmount : int = 96;
var yAmount : int = 128;
var caveX_Size : int = 1;
var caveY_Size : int = 2;
var update : boolean;
var newVertices : List.<Vector3> = new List.<Vector3>();
var colVertices : List.<Vector3> = new List.<Vector3>();
var newTriangles : List.<int> = new List.<int>();
var colTriangles : List.<int> = new List.<int>();
var newUV : List.<Vector2> = new List.<Vector2>();
//Here is the byte variable!
var blocks : byte[,];
//Hidden Variables--------------------------------------------------------
private var mesh : Mesh;
private var col : MeshCollider;
private var texUnit : float = 0.25;
private var texStone : Vector2 = new Vector2(1, 0);
private var texGrass : Vector2 = new Vector2(0, 1);
private var squareCount : int;
private var colCount : int;
function Start ()
{
mesh = GetComponent(MeshFilter).mesh;
col = GetComponent(MeshCollider);
GenTerrain();
BuildMesh();
UpdateMesh();
}
function Update ()
{
if(update)
{
BuildMesh();
UpdateMesh();
update = false;
}
}
function Noise (x : int, y : int, scale : float, mag : float, exp : float) : int
{
return Mathf.Pow((Mathf.PerlinNoise(x / scale, y / scale) * mag), (exp));
}
function GenTerrain ()
{
blocks = new byte[xAmount, yAmount];
for(var px : int = 0; px < blocks.GetLength(0); px++)
{
var stone : int = Noise(px, 0, 80, 15, 1);
stone += Noise(px, 0, 50, 30, 1);
stone += Noise(px, 0, 10, 10, 1);
stone += 75;
var dirt : int = Noise(px, 0, 100, 35, 1);
dirt += Noise(px, 0, 50, 30, 1);
dirt += 75;
for(var py : int = 0; py < blocks.GetLength(1); py++)
{
if(py < stone)
{
blocks[px, py] = 1;
if(Noise(px, py, 12, 16, 1) > 10)
{
blocks[px, py] = 2;
}
if(Noise(px * caveY_Size, py * caveX_Size, 16, 14, 1) > 10)
{
blocks[px, py] = 0;
}
}
else if (py < dirt)
{
blocks[px, py] = 2;
}
}
}
}
function BuildMesh ()
{
for (var px : int = 0; px < blocks.GetLength(0); px++)
{
for (var py : int = 0; py < blocks.GetLength(1); py++)
{
if(blocks[px, py] != 0)
{
GenCollider(px, py);
if(blocks[px, py] == 1)
{
GenSquare(px, py, texStone);
}
else if(blocks[px, py] == 2)
{
GenSquare(px, py, texGrass);
}
}
}
}
}
function Block (x : int, y : int) : byte
{
if(x == -1 || x == blocks.GetLength(0) || y == -1 || y == blocks.GetLength(1))
{
return 1;
}
return blocks[x, y];
}
function GenCollider (x : int, y : int)
{
//Top Face
if(Block(x, y + 1) == 0)
{
colVertices.Add(new Vector3(x, y, 1));//Back-left vertice
colVertices.Add(new Vector3(x + 1, y, 1));//Back-right vertice
colVertices.Add(new Vector3(x + 1, y, 0));//Front-right vertice
colVertices.Add(new Vector3(x, y, 0));//Front-left vertice
ColliderTriangles ();
colCount++;
}
//Bottom Face
if(Block(x, y - 1) == 0)
{
colVertices.Add(new Vector3(x, y - 1, 0));
colVertices.Add(new Vector3(x + 1, y - 1, 0));
colVertices.Add(new Vector3(x + 1, y - 1, 1));
colVertices.Add(new Vector3(x, y - 1, 1));
ColliderTriangles();
colCount++;
}
//Left Face
if(Block(x - 1, y) == 0)
{
colVertices.Add(new Vector3(x, y - 1, 1));
colVertices.Add(new Vector3(x, y, 1));
colVertices.Add(new Vector3(x, y, 0));
colVertices.Add(new Vector3(x, y - 1, 0));
ColliderTriangles();
colCount++;
}
//Right Face
if(Block(x + 1, y) == 0)
{
colVertices.Add(new Vector3(x + 1, y, 1));
colVertices.Add(new Vector3(x + 1, y - 1, 1));
colVertices.Add(new Vector3(x + 1, y - 1, 0));
colVertices.Add(new Vector3(x + 1, y, 0));
ColliderTriangles();
colCount++;
}
}
function ColliderTriangles ()
{
colTriangles.Add(colCount * 4);
colTriangles.Add((colCount * 4) + 1);
colTriangles.Add((colCount * 4) + 3);
colTriangles.Add((colCount * 4) + 1);
colTriangles.Add((colCount * 4) + 2);
colTriangles.Add((colCount * 4) + 3);
}
function GenSquare (x : int, y : int, texture : Vector2)
{
//This will create a simple square on the xy plane.
newVertices.Add(new Vector3(x, y, 0));
newVertices.Add(new Vector3(x + 1, y, 0));
newVertices.Add(new Vector3(x + 1, y - 1, 0));
newVertices.Add(new Vector3(x, y - 1, 0));
newTriangles.Add(squareCount * 4);
newTriangles.Add((squareCount * 4) + 1);
newTriangles.Add((squareCount * 4) + 3);
newTriangles.Add((squareCount * 4) + 1);
newTriangles.Add((squareCount * 4) + 2);
newTriangles.Add((squareCount * 4) + 3);
newUV.Add(new Vector2(texUnit * texture.x, texUnit * texture.y + texUnit));
newUV.Add(new Vector2(texUnit * texture.x + texUnit, texUnit * texture.y + texUnit));
newUV.Add(new Vector2(texUnit * texture.x + texUnit, texUnit * texture.y));
newUV.Add(new Vector2(texUnit * texture.x, texUnit * texture.y));
squareCount++;
}
function UpdateMesh ()
{
mesh.Clear();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize();
mesh.RecalculateNormals();
newVertices.Clear();
newTriangles.Clear();
newUV.Clear();
squareCount = 0;
var newMesh : Mesh = new Mesh();
newMesh.vertices = colVertices.ToArray();
newMesh.triangles = colTriangles.ToArray();
col.sharedMesh = newMesh;
colVertices.Clear();
colTriangles.Clear();
colCount = 0;
}