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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scripted plane not showing up

Discussion in 'Scripting' started by LordMikeVI, Jan 12, 2016.

  1. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    I need to generate a procedural mesh for my project so i first tried to simply script a plane. The problem is that the plane is not visible in the game or the scene view. Please help me figure out what I did wrong.
    Here is the script in C#
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlaneCreation : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.  
    10.     MeshFilter mf = GetComponent<MeshFilter>();
    11.     Mesh mesh = new Mesh();
    12.     mf.mesh = mesh;
    13.  
    14.  
    15.  
    16.         Vector3[] wv = new Vector3[4];
    17.  
    18.         wv[0] = new Vector3(-2,0,0);
    19.         wv[1] = new Vector3(-2,4,0);
    20.         wv[2] = new Vector3(2,0,0);
    21.         wv[3] = new Vector3(2,4,0);
    22.  
    23.         mesh.vertices = wv;
    24.  
    25.  
    26.         int[] tri = new int[6];
    27.  
    28.         tri[0] = 0;
    29.         tri[0] = 1;
    30.         tri[0] = 2;
    31.  
    32.         tri[0] = 1;
    33.         tri[0] = 3;
    34.         tri[0] = 2;
    35.  
    36.         mesh.triangles = tri;
    37.  
    38.  
    39.         Vector3[] normals = new Vector3[4];
    40.  
    41.         normals[0] = -Vector3.forward;
    42.         normals[1] = -Vector3.forward;
    43.         normals[2] = -Vector3.forward;
    44.         normals[3] = -Vector3.forward;
    45.  
    46.         mesh.normals = normals;
    47.  
    48.  
    49.         Vector2[] uv = new Vector2[4];
    50.  
    51.         uv[0] = new Vector2(0,0);
    52.         uv[0] = new Vector2(1,0);
    53.         uv[0] = new Vector2(0,1);
    54.         uv[0] = new Vector2(1,1);
    55.  
    56.         mesh.uv = uv;
    57.  
    58.     }
    59.    
    60.     // Update is called once per frame
    61.     void Update () {
    62.    
    63.     }
    64. }
    65.  
    Thanks in advance for the help.
     
  2. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    Can you see it in the hierarchy ? (does the object exist). If it exit, try to flip it : plane are visible in only one side
     
  3. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    Well I created an empty Object and added the Script, a Mesh Renderer and a Mesh Filter to it. If that is what you mean I so See it in the hierarchy.
    I already tried switching the normals but it didn't help.
    Maybe I am doing something wrong with the empty Game Object?