Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help Regarding Updating Unity and Script Errors

Discussion in 'Scripting' started by Jackk_, Nov 11, 2018.

  1. Jackk_

    Jackk_

    Joined:
    May 2, 2017
    Posts:
    49
    Around 5 years ago I made a procedural voxel-based (cubic) terrain generation engine in unity 5. Today I attempted to open said engine in a later version of unity (as i thought i made it in 5.4.1 not a previous version). Now all the scripts have been updated and some errors are arising during terrain generation.

    There is only one main error that keeps repeating which is regarding an object reference not set to the instance of an object. The error is as follows:
    Code (CSharp):
    1. System.NullReferenceException: Object reference not set to an instance of an object
    2.   at TerrainEngine.VoxelBlock.Generate (TerrainEngine.TerrainBlock Block, TerrainEngine.TempMeshData Info, .IMeshData IData, TerrainEngine.VoxelTempData Datas, Int32 StartX, Int32 StartY, Int32 StartZ, Int32 SizeX, Int32 SizeY, Int32 SizeZ, Int32 LodSize, Boolean OptimizeBorder) [0x00440] in E:\Unity Assets\TerrainEngineOS\Assets\TerrainEngine\Engines\Voxels\Core\VoxelBlock.cs:314
    3.   at TerrainEngine.VoxelBlock.Generate (.ActionThread Thread, TerrainEngine.TempMeshData& Info, .IMeshData& Data, Int32 ChunkId) [0x00092] in E:\Unity Assets\TerrainEngineOS\Assets\TerrainEngine\Engines\Voxels\Core\VoxelBlock.cs:126
    4.   at TerrainEngine.TerrainBlock.GenerateChunk (.ActionThread Thread, Int32 ChunkId) [0x00012] in E:\Unity Assets\TerrainEngineOS\Assets\TerrainEngine\Scripts\Terrain\TerrainBlock.cs:689
    5.   at TerrainEngine.TerrainBuilder+TerrainBlockBuilder.CreateBlock (.ActionThread Thread) [0x00244] in E:\Unity Assets\TerrainEngineOS\Assets\TerrainEngine\Scripts\Terrain\TerrainBuilder.cs:412
    6. UnityEngine.Debug:LogError(Object)
    7. GeneralLog:LogError(Object) (at Assets/TerrainEngine/Shared/Utils/GeneralLog.cs:73)
    8. TerrainEngine.TerrainBlockBuilder:CreateBlock(ActionThread) (at Assets/TerrainEngine/Scripts/Terrain/TerrainBuilder.cs:423)
    9. TerrainEngine.TerrainBlockBuilder:UpdateThread(ActionThread) (at Assets/TerrainEngine/Scripts/Terrain/TerrainBuilder.cs:97)
    10. TerrainEngine.TerrainBuilder:UpdateThread(ActionThread) (at Assets/TerrainEngine/Scripts/Terrain/TerrainBuilder.cs:674)
    11. ExecuteAction:OnExecute(ActionThread) (at Assets/TerrainEngine/Shared/Threading/Actions/ExecuteAction.cs:29)
    12. TerrainEngine.IAction:Execute(ActionThread) (at Assets/TerrainEngine/Shared/Threading/Actions/IAction.cs:34)
    13. ActionThread:Execute(Int32) (at Assets/TerrainEngine/Shared/Threading/ActionThread.cs:160)
    14. ActionThread:ThreadUpdate() (at Assets/TerrainEngine/Shared/Threading/ActionThread.cs:68)
    The code where the first error occurs is line 314 on the following script:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. namespace TerrainEngine
    6. {
    7.     [Serializable]
    8.     public class VoxelBlock : TerrainBlock
    9.     {
    10.         public VoxelBlock(TerrainManager Manager)
    11.             : base(Manager.Informations)
    12.         {
    13.  
    14.         }
    15.  
    16.         public byte GetVoxelTemp(ref VoxelTempInfo Info, int x, int y, int z, bool Safe)
    17.         {
    18.             if (Safe)
    19.             {
    20.                 Info.Type = Voxels.GetType(x, y, z);
    21.                 return Info.Type;
    22.             }
    23.  
    24.             ITerrainBlock AroundBlock = GetAroundBlock(ref x, ref y, ref z);
    25.             if (AroundBlock == null)
    26.             {
    27.                 Info.Border = true;
    28.                 return 0;
    29.             }
    30.  
    31.             Info.Type = AroundBlock.Voxels.GetType(x, y, z);
    32.             return Info.Type;
    33.         }
    34.  
    35.  
    36.         public VoxelTempData GetTempData( ActionThread Thread,  int StartX, int StartY, int StartZ, int SizeX, int SizeY, int SizeZ, int LodSize)
    37.         {
    38.             bool sx, sy, sz = false;
    39.             int px, py, pz = 0;
    40.  
    41.             VoxelTempData Datas = Thread.Pool.GetData<VoxelTempData>("VoxelTempData" + LodSize);
    42.             VoxelTempInfo Info = null;
    43.             Datas.Init((SizeX / LodSize) + 2, (SizeY / LodSize) + 2, (SizeZ / LodSize) + 2);
    44.             BlockInformation LastBlockInfo = null;
    45.             byte LastBlockType = 0;
    46.  
    47.             Datas.IsEmpty = true;
    48.             Datas.IsFull = true;
    49.  
    50.             int x, y, z;
    51.             for (x = 0; x < (SizeX / LodSize) + 2; ++x)
    52.             {
    53.                 px = StartX + (x - 1) * LodSize;
    54.                 sx = Voxels.IsValid(px);
    55.  
    56.                 for (y = 0; y < (SizeY / LodSize)+ 2; ++y)
    57.                 {
    58.                     py = StartY + (y - 1) * LodSize;
    59.                     sy = Voxels.IsValid(py);
    60.  
    61.                     for (z = 0; z < (SizeZ / LodSize) + 2; ++z)
    62.                     {
    63.                         pz = StartZ + (z - 1) * LodSize;
    64.                         sz = Voxels.IsValid(pz);
    65.  
    66.                         Info = Datas[x, y, z];
    67.                         Info.Border = false;
    68.  
    69.                         if (GetVoxelTemp(ref Info, px, py, pz, (sx && sy && sz)) != 0)
    70.                         {
    71.                             if (x > 0 && y > 0 && z > 0 && x <= SizeX && y <= SizeY && z <= SizeZ)
    72.                                 Datas.IsEmpty = false;
    73.  
    74.                             if (Info.Type == LastBlockType)
    75.                                 Info.Info = LastBlockInfo;
    76.                             else
    77.                             {
    78.                                 Info.Info = TerrainManager.Instance.Palette.GetVoxelData<BlockInformation>(Info.Type);
    79.                                 LastBlockInfo = Info.Info;
    80.                                 LastBlockType = Info.Type;
    81.  
    82.                                 if (LastBlockInfo.VoxelType != VoxelTypes.OPAQUE)
    83.                                     Datas.IsFull = false;
    84.                             }
    85.                         }
    86.                         else
    87.                         {
    88.                             Info.Type = 0;
    89.                             Info.Info = null;
    90.                             Datas.IsFull = false;
    91.                         }
    92.                     }
    93.                 }
    94.             }
    95.  
    96.             return Datas;
    97.         }
    98.  
    99.         public override VectorI3 GetVoxelFromWorldPosition(Vector3 WorldPosition, bool Remove)
    100.         {
    101.             /*if (Remove)
    102.                 WorldPosition -= Normal * Information.Scale * 0.5f;
    103.             else
    104.                 WorldPosition += Normal * Information.Scale * 0.5f;
    105.             */
    106.             Vector3 blockPos = (WorldPosition - this.WorldPosition) / Information.Scale;
    107.             blockPos.x += 1;
    108.             blockPos.z += 1;
    109.             blockPos.y += 1;
    110.             return blockPos;
    111.         }
    112.  
    113.         public override void Generate(ActionThread Thread,ref TempMeshData Info,ref IMeshData Data, int ChunkId)
    114.         {
    115.             Info = Thread.Pool.UnityPool.TempChunkPool.GetData<TempMeshData>();
    116.             Data = PoolManager.UPool.GetSimpleMeshData();
    117.             Data.Materials = (TerrainManager.Instance as VoxelManager).WorldSharedMaterial;
    118.  
    119.             int SizeX = Information.VoxelPerChunk;
    120.             int SizeY = Information.VoxelPerChunk;
    121.             int SizeZ = Information.VoxelPerChunk;
    122.  
    123.             TerrainChunk Chunk = Chunks[ChunkId];
    124.             VoxelTempData Datas = GetTempData(Thread, Chunk.StartX, Chunk.StartY, Chunk.StartZ, SizeX, SizeY, SizeZ, 1);
    125.          
    126.             VoxelBlock.Generate(this, Info, Data, Datas, GetWorldVoxelX(Chunk.StartX), GetWorldVoxelY(Chunk.StartY), GetWorldVoxelZ(Chunk.StartZ), SizeX, SizeY, SizeZ, 1, TerrainManager.Instance.OptimizeBlockBorders);
    127.             Datas.Close();
    128.         }
    129.  
    130.         public override float InterpolateHeight(byte CurrentType, float TopVolume, float CurrentVolume)
    131.         {
    132.             return 0;
    133.         }
    134.  
    135.         public override void GetPositionOffset(ref float x, ref float y, ref float z)
    136.         {
    137.             x -= 0.5f * Information.Scale;
    138.             z -= 0.5f * Information.Scale;
    139.         }
    140.  
    141.         public IEnumerable BlockLoopNoAir(TerrainChunk Chunk)
    142.         {
    143.             int x, y, z;
    144.             for (x = 0; x < Information.VoxelPerChunk; x++)
    145.                 for (y = 0; y < Information.VoxelPerChunk; y++)
    146.                     for (z = 0; z < Information.VoxelPerChunk; z++)
    147.                     {
    148.                         if (Voxels.GetType(x + Chunk.StartX, y + Chunk.StartY, z + Chunk.StartZ) == BlockManager.None)
    149.                             continue;
    150.  
    151.                         yield return new VectorI3(x, y, z);
    152.                     }
    153.         }
    154.  
    155.         public IEnumerable BlockFaceLoop(VectorI3 index)
    156.         {
    157.             for (int f = 0; f < BlockHelper.facescount; f++)
    158.             {
    159.                 if (!BlockFaceCheckBounds(index, f)) continue;
    160.  
    161.                 yield return f;
    162.             }
    163.         }
    164.  
    165.         public bool BlockFaceCheckBounds(VectorI3 index, int face)
    166.         {
    167.             return GetNeighbor(index.x, index.y, index.z, face) == 0;
    168.         }
    169.  
    170.         public byte GetNeighbor(int x, int y, int z, int directionIndex, bool Safe = false)
    171.         {
    172.             return GetByte(x + BlockHelper.direction[directionIndex].x,
    173.                 y + BlockHelper.direction[directionIndex].y,
    174.                 z + BlockHelper.direction[directionIndex].z, Safe);
    175.         }
    176.  
    177.         public bool IsSafe(int px, int py, int pz)
    178.         {
    179.             return (px < Information.VoxelPerChunk - 1 && px >= 1)
    180.                     && (py < Information.VoxelPerChunk - 1 && py >= 1)
    181.                     && (pz < Information.VoxelPerChunk - 1 && pz >= 1);
    182.         }
    183.  
    184.         public bool[] GetNeighbors(VectorI3 index)
    185.         {
    186.             bool[] neighbors = new bool[26];
    187.             bool Safe = IsSafe(index.x, index.y, index.z);
    188.  
    189.             for (int i = 0; i < 26; i++)
    190.             {
    191.                 if (GetNeighbor(index.x, index.y, index.z, i, Safe) != 0)
    192.                     neighbors[i] = true;
    193.             }
    194.  
    195.             return neighbors;
    196.         }
    197.  
    198.         static public void Generate(TerrainBlock Block, TempMeshData Info, IMeshData IData, VoxelTempData Datas, int StartX, int StartY, int StartZ, int SizeX, int SizeY, int SizeZ, int LodSize, bool OptimizeBorder=true)
    199.         {
    200.             if (!Datas.IsFull && !Datas.IsEmpty)
    201.             {
    202.                 VoxelTempInfo VoxelInfo = null;
    203.                 VoxelTempInfo AroundInfo = null;
    204.                 ColorUVMeshData Data = IData as ColorUVMeshData;
    205.  
    206.                 byte BlockType = 0;
    207.                 bool Safe = false;
    208.                 BlockInformation BlockInfo = null;
    209.                 Color[] ambientColors = new Color[5];
    210.                 int i, g, f, foundNeighbors = 0;
    211.                 int px, py, pz;
    212.                 int x, y, z;
    213.                 BlockHelper.BlockFace Face;
    214.  
    215.                 for (x = 0; x < (SizeZ / LodSize); x++)
    216.                 {
    217.                     px = StartX + (x - 1) * LodSize;
    218.                     for (y = 0; y < (SizeY / LodSize); y++)
    219.                     {
    220.                         py = StartY + (y - 1) * LodSize;
    221.                         for (z = 0; z < (SizeX / LodSize); z++)
    222.                         {
    223.                             pz = StartZ + (z - 1) * LodSize;
    224.                             VoxelInfo = Datas[x + 1, y + 1, z + 1];
    225.  
    226.                             BlockType = VoxelInfo.Type;
    227.  
    228.                             if ((BlockType = VoxelInfo.Type) == 0)
    229.                                 continue;
    230.  
    231.                             foundNeighbors = 0;
    232.                             BlockInfo = VoxelInfo.Info;
    233.  
    234.                             if (BlockInfo == null || BlockInfo.VoxelType == VoxelTypes.FLUID)
    235.                                 continue;
    236.  
    237.                             for (i = 0; i < BlockHelper.facescount; ++i)
    238.                             {
    239.                                 AroundInfo = VoxelInfo.Arounds[i];
    240.                                 if (AroundInfo.Type != BlockManager.None && AroundInfo.Info.VoxelType != VoxelTypes.FLUID)
    241.                                 {
    242.                                     ++foundNeighbors;
    243.                                     Datas.Neighbors[i] = true;
    244.                                 }
    245.                                 else
    246.                                     Datas.Neighbors[i] = false;
    247.                             }
    248.  
    249.                             if (foundNeighbors == 0) continue;
    250.  
    251.                             Safe = false;
    252.  
    253.                             for (f = BlockHelper.facescount-1; f >= 0; --f)
    254.                             {
    255.                                 if (Datas.Neighbors[f] == true)
    256.                                     continue;
    257.  
    258.                                 if (OptimizeBorder && VoxelInfo.Arounds[f].Border)
    259.                                     continue;
    260.  
    261.                                 if (Safe == false)
    262.                                 {
    263.                                     Safe = true;
    264.  
    265.                                     for (i = BlockHelper.facescount; i < 26; ++i)
    266.                                     {
    267.                                         AroundInfo = VoxelInfo.Arounds[i];
    268.                                         if (AroundInfo.Type != 0 && AroundInfo.Info.VoxelType != VoxelTypes.FLUID)
    269.                                             Datas.Neighbors[i] = true;
    270.                                         else
    271.                                             Datas.Neighbors[i] = false;
    272.                                     }
    273.                                 }
    274.  
    275.                                 VoxelAmbient.ChangeLight(ref ambientColors, f, Datas.Neighbors);
    276.                                 Face = BlockHelper.facesEnum[f];
    277.  
    278.                                 for (g = BlockInfo.ColorCount-1; g >= 0; --g)
    279.                                 {
    280.                                     if (BlockInfo.ColorGenerators[g].IsAllowedFace(Face))
    281.                                     {
    282.                                         ambientColors[0] = BlockInfo.ColorGenerators[g].GenerateColor(px, py, pz, f, ambientColors[0]);
    283.                                         ambientColors[0].a = 1;
    284.  
    285.                                         ambientColors[1] = BlockInfo.ColorGenerators[g].GenerateColor(px, py, pz, f, ambientColors[1]);
    286.                                         ambientColors[1].a = 1;
    287.  
    288.                                         ambientColors[2] = BlockInfo.ColorGenerators[g].GenerateColor(px, py, pz, f, ambientColors[2]);
    289.                                         ambientColors[2].a = 1;
    290.  
    291.                                         ambientColors[3] = BlockInfo.ColorGenerators[g].GenerateColor(px, py, pz, f, ambientColors[3]);
    292.                                         ambientColors[3].a = 1;
    293.  
    294.                                         ambientColors[4] = BlockInfo.ColorGenerators[g].GenerateColor(px, py, pz, f, ambientColors[4]);
    295.                                         ambientColors[4].a = 1;
    296.                                     }
    297.                                 }
    298.  
    299.                                 AddFace(Block, BlockType, x, y, z, f,(int)Face, ref Info, (float)(Block.Information.Scale * LodSize) + 0.0001f);
    300.  
    301.                                 if (ambientColors != null)
    302.                                     Info.Colors.AddRange(ambientColors);
    303.                             }
    304.                         }
    305.                     }
    306.                 }
    307.  
    308.                 if (Info.Triangles.Indices.Count > 0)
    309.                 {
    310.                     Info.CurrentIndices = new int[1][];
    311.                     Info.CurrentIndices[0] = Info.Triangles.Indices.ToArray();
    312.                     Info.GenerateNormals();
    313.  
    314.                     Data.CColors = Info.Colors.ToArray();
    315.                     Data.CNormals = Info.Normals.ToArray();
    316.                     Data.CIndices = Info.CurrentIndices;
    317.                     Data.CUVs = Info.Uvs.ToArray();
    318.                     Data.CVertices = Info.Vertices.ToArray();
    319.                     Data.CMaterials = (TerrainManager.Instance as VoxelManager).WorldSharedMaterial;
    320.                 }
    321.             }
    322.         }
    323.  
    324.         static public void AddFace(TerrainBlock Block, byte BlockType, int x, int y, int z, int f,int Face, ref TempMeshData Info, float Scale, float Height = 0, float Offset=0)
    325.         {
    326.             Vector3 midvertex = new Vector3();
    327.             int i = 0;
    328.             Info.Vertices.CheckArray(4);
    329.             Vector3 vertex;
    330.             Height *= Scale;
    331.             Offset *= Scale;
    332.             Vector3[] vertices = BlockHelper.faceVertices[f];
    333.             for (i = 0; i < BlockHelper.verticescount - 1; ++i)
    334.             {
    335.                 vertex = vertices[i] * Scale;
    336.                 vertex.x += (x - 1) * Scale;
    337.                 vertex.y += (y - 1) * Scale;
    338.                 vertex.z += (z - 1) * Scale;
    339.  
    340.                 if (Height != 0 && f != 3)
    341.                 {
    342.                     if (i == 0 || i == 1)
    343.                         vertex.y -= Height;
    344.                     else if ((i == 2 || i == 3) && f == 2)
    345.                         vertex.y -= Height;
    346.  
    347.                     if (i > 1 && f != 2)
    348.                         vertex.y += Offset;
    349.                 }
    350.  
    351.                 midvertex += vertex;
    352.                 Info.Vertices.AddSafe(vertex);
    353.             }
    354.  
    355.             midvertex *= 0.25f;
    356.             Info.Vertices.Add(midvertex);
    357.  
    358.             Info.Triangles.Indices.CheckArray(12);
    359.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[0] + Info.TriangleIndex));
    360.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[1] + Info.TriangleIndex));
    361.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[2] + Info.TriangleIndex));
    362.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[3] + Info.TriangleIndex));
    363.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[4] + Info.TriangleIndex));
    364.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[5] + Info.TriangleIndex));
    365.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[6] + Info.TriangleIndex));
    366.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[7] + Info.TriangleIndex));
    367.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[8] + Info.TriangleIndex));
    368.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[9] + Info.TriangleIndex));
    369.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[10] + Info.TriangleIndex));
    370.             Info.Triangles.Indices.AddSafe((BlockHelper.triangles[11] + Info.TriangleIndex));
    371.             x = Face;
    372.  
    373.             Info.Uvs.CheckArray(5);
    374.             Info.Uvs.AddSafe(TerrainManager.Instance.Palette.GetUv((int)BlockType, x, 0));
    375.             Info.Uvs.AddSafe(TerrainManager.Instance.Palette.GetUv((int)BlockType, x, 1));
    376.             Info.Uvs.AddSafe(TerrainManager.Instance.Palette.GetUv((int)BlockType, x, 2));
    377.             Info.Uvs.AddSafe(TerrainManager.Instance.Palette.GetUv((int)BlockType, x, 3));
    378.             Info.Uvs.AddSafe(TerrainManager.Instance.Palette.GetUv((int)BlockType, x, 4));
    379.  
    380.             Info.TriangleIndex += BlockHelper.verticescount;
    381.         }
    382.     }
    383. }
    384.  
    Any help would be greatly appreciated as i do not know what is causing this error (note that this is being ran in unity 5.4.1)
    Also feel free to ask to see any other information that I haven't yet shown
     
  2. Jackk_

    Jackk_

    Joined:
    May 2, 2017
    Posts:
    49