I'm Getting the "Missing Field Exception" error for System.Byte...Why?

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;
}

Please, post the complete error message.

ok, sorry: And the error line on this post is different. Line 23

MissingFieldException: System.Byte[,].
Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory.ResolveMember ()
Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory.CreateSetter ()
Boo.Lang.Runtime.RuntimeServices.CreateSetSliceDispatcher (System.Object target, System.String name, System.Object[ ] args)
Boo.Lang.Runtime.RuntimeServices+c__AnonStorey1E.<>m__15 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.SetSlice (System.Object target, System.String name, System.Object[ ] args)
Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory+c__AnonStorey13.<>m__6 (System.Object o, System.Object[ ] arguments)
Boo.Lang.Runtime.RuntimeServices.SetSlice (System.Object target, System.String name, System.Object[ ] args)
PolyDestroyer.Update () (at Assets/Resources/KeenansCoolResources/KeenansCoolScripts/VoxelGeneration/PolyDestroyer.js:31)

I am not a UnityScript expert. I would try a few thing out, like trying to create a setter function, instead of manipulating the array directly. Make sure the compiler knows that 1 is supposed to by of type byte, e.g. by creating a variable before the call, assign one to it and then assign that variable of type byte. I am really just guessing here :slight_smile:

Is there a reason, why you are not using #pragma strict?

I’m not using pragma strict because…idk, I’ve just never used it. Should I? I thought it just limited you. For you answer, would I declare the variable in the script that deletes squares or the square creator script? What variable type and value would it have so that Unity compiles everything correctly? Sorry for all the questions :slight_smile:

Yes, always keep #pragma strict at the top of all scripts. It’s put there by default and shouldn’t be removed. (It’s automatically enforced on some platforms such as iOS; honestly it should probably just be enforced on all of them.)

You shouldn’t write code like “var tScript;” (always declare the type, either explicitly, or implicitly by using a value), and also shouldn’t use strings in GetComponent.

–Eric

ok cool. Unfortunatley, for some reason I can’t make my tScript variable’s type the square generator script. I get an error. I originally had the variable tScript : 2DGeneratorScript. I got errors though.

Assets/Resources/KeenansCoolResources/KeenansCoolScripts/VoxelGeneration/PolyDestroyer.js(6,23): BCE0043: Unexpected token: 2D.

Assets/Resources/KeenansCoolResources/KeenansCoolScripts/VoxelGeneration/PolyDestroyer.js(6,25): UCE0001: ‘;’ expected. Insert a semicolon at the end.

And when I take the GetComponent argument out of the quotes, i get another 2 errors. I originnaly did most of the stuff you pointed out but got errors for some reason. Here are the two errors from making the argument not a string

Assets/Resources/KeenansCoolResources/KeenansCoolScripts/VoxelGeneration/PolyDestroyer.js(12,42): BCE0044: expecting ), found ‘GeneratorScript’.

Assets/Resources/KeenansCoolResources/KeenansCoolScripts/VoxelGeneration/PolyDestroyer.js(12,57): BCE0043: Unexpected token: ).

wtf, I changed the script names to “PolygonGeneratorScript” and “PolygonDestroyerScript” and it worked (I also changed the naes of the variable type and GetComponent argument accordingly). Was it because one of the script has a number in it?

Sort of; numbers are OK as long as you don’t start with one. So “Blah2D” is fine but “2DBlah” isn’t.

–Eric

Ok that answered my problem, thanks and lesson learned :slight_smile: