Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem with Vector3Int

Discussion in 'Scripting' started by Xander_8002, Jul 18, 2022.

  1. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    I am following a tutorial for minecraft like terrain generation and there is a problem. I am getting an error message which tells me that Vector3Int doesnt have a definition for forward or back. Here are the two pieces of code.
    Holds all Vector3Int variables:
    Code (CSharp):
    1.     static readonly Vector3Int[] CheckDirections = new Vector3Int[]
    2.     {
    3.         Vector3Int.up,
    4.         Vector3Int.down,
    5.         Vector3Int.forward,
    6.         Vector3Int.back,
    7.         Vector3Int.left,
    8.         Vector3Int.right
    9.     };
    Where the problem seems to come from:
    Code (CSharp):
    1. if(CheckDirections[i] == Vector3Int.up)
    2.             {
    3.                 CubeFaces.Add(CheckDirections[i], new FaceData(UpFace, UpTris)); }
    4.             else if(CheckDirections[i] == Vector3Int.down)
    5.             {
    6.                CubeFaces.Add(CheckDirections[i], new FaceData(DownFace, DownTris));
    7.             }
    8.             else if(CheckDirections[i] == Vector3Int.forward)
    9.             {
    10.                CubeFaces.Add(CheckDirections[i], new FaceData(ForwardFace, ForwardTris));
    11.             }
    12.             else if(CheckDirections[i] == Vector3Int.back)
    13.             {
    14.                 CubeFaces.Add(CheckDirections[i], new FaceData(BackFace, BackTris));
    15.             }
    16.             else if(CheckDirections[i] == Vector3Int.left)
    17.             {
    18.                 CubeFaces.Add(CheckDirections[i], new FaceData(LeftFace, LeftTris));
    19.             }
    20.             else if(CheckDirections[i] == Vector3Int.right)
    21.             {
    22.                 CubeFaces.Add(CheckDirections[i], new FaceData(RightFace, RightTris));
    23.             }
    I am not sure why there is a problem here?

    If anyone needs it, here is all 200 ish lines of code in this script. I have also put the relevant sections in bold just so you can see where they are if that is of any relevance to the problem.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChunkMeshCreator
    6. {
    7.     public class FaceData
    8.     {
    9.         public FaceData(Vector3[] verts, int[] tris)
    10.         {
    11.             Vertices = verts;
    12.             Indices = tris;
    13.         }
    14.  
    15.         public Vector3[] Vertices;
    16.         public int[] Indices;
    17.     }
    18.  
    19.  
    20.     static readonly Vector3Int[] CheckDirections = new Vector3Int[]
    21.     {
    22. [B]        Vector3Int.up,
    23.         Vector3Int.down,
    24.         Vector3Int.forward,
    25.         Vector3Int.back,
    26.         Vector3Int.left,
    27.         Vector3Int.right[/B]
    28.     };
    29.  
    30.     static readonly Vector3[] RightFace = new Vector3[]
    31.     {
    32.         new Vector3(.5f, -.5f, -.5f),
    33.         new Vector3(.5f, -.5f, .5f),
    34.         new Vector3(.5f, .5f, .5f),
    35.         new Vector3(.5f, .5f, -.5f)
    36.     };
    37.  
    38.     static readonly int[] RightTris = new int[]
    39.     {
    40.         0,2,1,0,3,2
    41.     };
    42.  
    43.     static readonly Vector3[] LeftFace = new Vector3[]
    44.     {
    45.         new Vector3(-.5f, -.5f, -.5f),
    46.         new Vector3(-.5f, -.5f, .5f),
    47.         new Vector3(-.5f, .5f, .5f),
    48.         new Vector3(-.5f, .5f, -.5f)
    49.     };
    50.  
    51.     static readonly int[] LeftTris = new int[]
    52.     {
    53.         0,1,2,0,2,3
    54.     };
    55.  
    56.     static readonly Vector3[] UpFace = new Vector3[]
    57.     {
    58.         new Vector3(-.5f, .5f, -.5f),
    59.         new Vector3(-.5f, .5f, .5f),
    60.         new Vector3(.5f, .5f, .5f),
    61.         new Vector3(.5f, .5f, -.5f)
    62.     };
    63.  
    64.     static readonly int[] UpTris = new int[]
    65.     {
    66.         0,1,2,0,2,3
    67.     };
    68.  
    69.     static readonly Vector3[] DownFace = new Vector3[]
    70.     {
    71.         new Vector3(-.5f, -.5f, -.5f),
    72.         new Vector3(-.5f, -.5f, .5f),
    73.         new Vector3(.5f, -.5f, .5f),
    74.         new Vector3(.5f, -.5f, -.5f)
    75.     };
    76.  
    77.     static readonly int[] DownTris = new int[]
    78.     {
    79.         0,2,1,0,3,2
    80.     };
    81.  
    82.     static readonly Vector3[] ForwardFace = new Vector3[]
    83.     {
    84.         new Vector3(-.5f, -.5f, .5f),
    85.         new Vector3(-.5f, .5f, .5f),
    86.         new Vector3(.5f, .5f, .5f),
    87.         new Vector3(.5f, -.5f, .5f)
    88.     };
    89.  
    90.     static readonly int[] ForwardTris = new int[]
    91.     {
    92.         0,2,1,0,3,2
    93.     };
    94.  
    95.     static readonly Vector3[] BackFace = new Vector3[]
    96.     {
    97.         new Vector3(-.5f, -.5f, -.5f),
    98.         new Vector3(-.5f, .5f, -.5f),
    99.         new Vector3(.5f, .5f, -.5f),
    100.         new Vector3(.5f, -.5f, -.5f)
    101.     };
    102.  
    103.     static readonly int[] BackTris = new int[]
    104.     {
    105.         0,1,2,0,2,3
    106.     };
    107.  
    108.     private Dictionary<Vector3Int, FaceData> CubeFaces = new Dictionary<Vector3Int, FaceData>();
    109.  
    110.     public ChunkMeshCreator()
    111.     {
    112.         CubeFaces = new Dictionary<Vector3Int, FaceData>();
    113.  
    114.         for(int i = 0; i < CheckDirections.Length; i++)
    115.         {
    116. [B]            if(CheckDirections[i] == Vector3Int.up)
    117.             {
    118.                 CubeFaces.Add(CheckDirections[i], new FaceData(UpFace, UpTris)); }
    119.             else if(CheckDirections[i] == Vector3Int.down)
    120.             {
    121.                CubeFaces.Add(CheckDirections[i], new FaceData(DownFace, DownTris));
    122.             }
    123.             else if(CheckDirections[i] == Vector3Int.forward)
    124.             {
    125.                CubeFaces.Add(CheckDirections[i], new FaceData(ForwardFace, ForwardTris));
    126.             }
    127.             else if(CheckDirections[i] == Vector3Int.back)
    128.             {
    129.                 CubeFaces.Add(CheckDirections[i], new FaceData(BackFace, BackTris));
    130.             }
    131.             else if(CheckDirections[i] == Vector3Int.left)
    132.             {
    133.                 CubeFaces.Add(CheckDirections[i], new FaceData(LeftFace, LeftTris));
    134.             }
    135.             else if(CheckDirections[i] == Vector3Int.right)
    136.             {
    137.                 CubeFaces.Add(CheckDirections[i], new FaceData(RightFace, RightTris));
    138.             }[/B]
    139.         }
    140.     }
    141.  
    142.     public Mesh CreateMeshFromData(int[,,] Data)
    143.     {
    144.         List<Vector3> Vertices = new List<Vector3>();
    145.         List<int> Indices = new List<int>();
    146.         Mesh m = new Mesh();
    147.  
    148.         for(int x =  0; x < WorldGenerator.ChunkSize.x; x++)
    149.         {
    150.             for(int y =  0; y < WorldGenerator.ChunkSize.y; y++)
    151.             {
    152.                 for(int z =  0; z < WorldGenerator.ChunkSize.z; z++)
    153.                 {
    154.                     Vector3Int BlockPos = new Vector3Int(x, y, z);
    155.                     for(int i = 0; i < CheckDirections.Length; i++)
    156.                     {
    157.                         Vector3Int BlockToCheck = BlockPos + CheckDirections[i];
    158.  
    159.                         try
    160.                         {
    161.                             if(Data[BlockToCheck.x, BlockToCheck.y, BlockToCheck.z] == 0)
    162.                             {
    163.                                 if(Data[BlockPos.x, BlockPos.y, BlockPos.z] == 0)
    164.                                 {
    165.                                     FaceData FaceToApply = CubeFaces[CheckDirections[i]];
    166.  
    167.                                     foreach(Vector3 vert in FaceToApply.Vertices)
    168.                                     {
    169.                                         Vertices.Add(new vector3(x,y,z) + vert);
    170.                                     }
    171.  
    172.                                     foreach(int tri in FaceToApply.Indices)
    173.                                     {
    174.                                         Indices.Add(Vertices.Count - 4 + tri);
    175.                                     }
    176.                                 }
    177.                             }
    178.                         }
    179.                         catch(System.Exception)
    180.                         {
    181.                             if(Data[BlockPos.x, BlockPos.y, BlockPos.z] == 0)
    182.                             {
    183.                                 FaceData FaceToApply = CubeFaces[CheckDirections[i]];
    184.  
    185.                                 foreach(Vector3 vert in FaceToApply.Vertices)
    186.                                 {
    187.                                     Vertices.Add(new vector3(x,y,z) + vert);
    188.                                 }
    189.  
    190.                                 foreach(int tri in FaceToApply.Indices)
    191.                                 {
    192.                                     Indices.Add(Vertices.Count - 4 + tri);
    193.                                 }
    194.                             }
    195.                         }
    196.                     }
    197.                 }
    198.             }
    199.         }
    200.    
    201.     m.SetVertices(Vertices);
    202.     m.SetIndices(Indices, MeshTopology.Triangles, 0);
    203.  
    204.     m.RecalculateBounds();
    205.     m.RecalculateTangents();
    206.     m.RecalculateNormals();
    207.  
    208.     return m;
    209.    
    210.     }
    211. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Those properties definitely exist on UnityEngine.Vector3Int:
    https://docs.unity3d.com/ScriptReference/Vector3Int.html

    My guess is that maybe you made your own script named Vector3Int, and it's picking up your script instead of Unity's built in struct? Where does your IDE take you if you jump to the definition of Vector3Int from where the error is?
     
    Kurt-Dekker likes this.
  3. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    The IDEs take me to 24,20 and 25,20 which is
    Vector3Int.forward
    and
    Vector3Int.back
    it takes me specifically to after the fullstop

    The thing that confuses me also is that the tutorial isn't even that old. It is only 6 months ago. I did a tutorial a few days ago that was over 6 years old i think. I don't know why it is doing this but i don't think it is a problem with the age of the code.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Can you share exactly what the error message you receive is? And where you are receiving it? Is it in Unity's console, or just in your IDE?
     
  5. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    It is from Unity's console
    Assets\ChunkMeshCreator.cs(123,54): error CS0117: 'Vector3Int' does not contain a definition for 'forward'
    and
    Assets\ChunkMeshCreator.cs(127,54): error CS0117: 'Vector3Int' does not contain a definition for 'back'

    and then
    Assets\ChunkMeshCreator.cs(123,54): error CS0117: 'Vector3Int' does not contain a definition for 'forward'
    and
    Assets\ChunkMeshCreator.cs(127,54): error CS0117: 'Vector3Int' does not contain a definition for 'back'
     
    Last edited: Jul 18, 2022
  6. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    Came up with a solution. instead of having
    Vector3Int.forward
    I now have
    new Vector3Int(0, 0, 1)
    which works just the same
     
  7. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,690
    Like PraetorBlue said, I think there’s a good chance your project declares another Vector3Int struct/class somewhere else, that does not define “up”, “forward”, etc. Then the compiler is picking up this Vector3Int instead of the one declared in the UnityEngine namespace.
     
  8. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    I had multi[le looks through the whole game and couldnt find a single other Vector3Int that would cause such a problem. However, there has been a few other forum threads that i found where people had the same problem. I think it might be something to do with the version of Unity I am on. Thanks for all the help though guys. :)
     
    arkano22 likes this.
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    For posterity, which version is that?
     
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    That's not really possible because you do have another Vector3Int type defined somewhere in your project or you use an too old Unity version. That other type could have been added through a managed dll or inside a script.

    So the type you're using is probably not in the one in the UnityEngine namespace. It's most likely a custom implementation that you have imported somewhere. Maybe you're using a very old package / framework that has shipped it's own Vector3Int struct that was created before Unity had introduced this struct (though this would be several years old). I actually made my own Vector3Int probably 7 years ago before Unity had its own,

    When PraetorBlue said this:
    He meant, if you use visual studio, you should put your cursor on "Vector3Int", right click and select "Go To Definition" from the context menu:
    upload_2022-7-19_10-16-17.png
    Alternatively press F12. This should bring you to a type definition that is reconstructed from the metadata that looks like this:

    upload_2022-7-19_10-18-12.png

    If it does not, the type you're using is not the one from the UnityEngine namespace. When I just hover over the name, I get this type popup / tooltip:
    upload_2022-7-19_10-14-46.png

    As you can see it clearly says
    UnityEngine.Vector3Int
    . Chances are high that this is not what you're seeing.

    If the type actually is UnityEngine.Vector3Int, then you must be using an older Unity version. In the past the static properties did not exist. They were added in Unity 2020.2+:

     
    PraetorBlue likes this.