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 visible

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

  1. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    So I wanted to code a simple plane with this code:
    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.  
    But the plane is not visible. I already tried flipping the normals and/or rotating the empty game Object but that did nothing.
    Here is the empty game Object the script is attached too. Screen Shot 2016-01-13 at 14.30.17.png
    And here a screen shot of my camera in case something is wrong there.
    Screen Shot 2016-01-13 at 14.31.47.png
    Any idea how to fix this?
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Between line 28 and 35, you only change the first element of the triangle indices.
     
    LordMikeVI likes this.
  3. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    Now I feel realy dumb. But thanks a lot!