Search Unity

"You are trying to create MonoBehaviour using the ''new' keyword.

Discussion in 'Scripting' started by LivingClockwork, Feb 14, 2016.

  1. LivingClockwork

    LivingClockwork

    Joined:
    Feb 12, 2016
    Posts:
    4
    It mentions:
    "UnityEngine.MonoBehaviour:.ctor()"
    "TileCord:.ctor()"
    "Tile:.ctor()"

    I get the problem, I don't get how to fix it.

    Code (csharp):
    1.  
    2. public class Tile : MonoBehaviour {
    3.     public float x = 0f;
    4.     public float z = 0f;
    5.     public float spacing = 1.0f;
    6.  
    7.  
    8.     public class TileCord : MonoBehaviour {
    9.         public float UR = 0f; //Upper Right
    10.         public float DR = 0f; //Down Right
    11.         public float UL = 0f; //Upper Left
    12.         public float DL = 0f; //Down Left
    13.         public float Cen = 0f; //Center
    14.        
    15.        
    16.     }
    17.    
    18.     public TileCord TileC = new TileCord();
    19.    
    20.     void Start () {
    21.         ///Mesh Creation
    22.         Mesh TileMesh = new Mesh ();
    23.         MeshFilter mf = GetComponent<MeshFilter> ();
    24.         //TileMesh.name = "Tile_x:" + (x.ToString) + "_z:" + (z.ToString);
    25.         mf.mesh = TileMesh;
    26.        
    27.         ///Vertices
    28.         Vector3[] verticies = new Vector3[5]
    29.         {
    30.             new Vector3 (x + spacing, TileC.UR, z + spacing), //Upper Right
    31.             new Vector3 (x + spacing, TileC.DR, z - spacing), //Down Right
    32.             new Vector3 (x - spacing, TileC.UL, z + spacing), //Upper Left
    33.             new Vector3 (x - spacing, TileC.DL, z - spacing), //Down Left
    34.             new Vector3 (x, TileC.Cen, z) //Center
    35.         };
    36.        
    37.        
    38.         ///Triangles
    39.         int[] tri = new int[12];
    40.         // 1
    41.         tri[0] = 0;
    42.         tri[1] = 1;
    43.         tri[2] = 4;
    44.         // 2
    45.         tri[3] = 3;
    46.         tri[4] = 2;
    47.         tri[5] = 4;
    48.         // 3
    49.         tri[6] = 2;
    50.         tri[7] = 0;
    51.         tri[8] = 4;
    52.         // 4
    53.         tri[9] = 1;
    54.         tri[10]= 3;
    55.         tri[11]= 4;
    56.        
    57.         ///Normals
    58.         Vector3[] normals = new Vector3[5];
    59.         normals[0] = -Vector3.up;
    60.         normals[1] = -Vector3.up;
    61.         normals[2] = -Vector3.up;
    62.         normals[3] = -Vector3.up;
    63.         normals[4] = -Vector3.up;
    64.        
    65.        
    66.         ///UV
    67.         Vector2[] uv = new Vector2[5];
    68.        
    69.         uv[0] = new Vector2 (1, 1);
    70.         uv[1] = new Vector2 (1, 0);
    71.         uv[2] = new Vector2 (0, 1);
    72.         uv[3] = new Vector2 (0, 0);
    73. //        uv[4] = new Vector2 (0.5, 0.5);
    74.        
    75.         ///Assaign Arrays
    76.         TileMesh.vertices = verticies;
    77.         TileMesh.triangles = tri;
    78.         TileMesh.normals = normals;
    79.         TileMesh.uv = uv;
    80.     }
    81. }
    82.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Don't derive TileCord from MonoBehaviour - it looks like it's just meant to be a standalone class.

    If you did that so that you can edit its properties in the inspector, do this instead:
    Code (csharp):
    1. [System.Serializable]
    2. public class TileCord {
     
    Kiwasi and LivingClockwork like this.
  3. LivingClockwork

    LivingClockwork

    Joined:
    Feb 12, 2016
    Posts:
    4
    Great! Worked. Thanks for the quick reply.