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

Problem with generating a mesh behind an Object

Discussion in 'Scripting' started by LordMikeVI, Feb 2, 2016.

  1. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    Hello,
    I have a problem with generating a wall behind a moving object by scripting it.
    The Errors are here: Screen Shot 2016-02-02 at 09.41.12.png

    The Code is as following in C#
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    6.  
    7. public class WallScript : MonoBehaviour {
    8.  
    9.  
    10.     public GameObject Wall;
    11.     public float height = 1;
    12.     private int z = 1;
    13.     private int i = 0;
    14.     private bool newcalc = false;
    15.     private Mesh mesh;
    16.  
    17.  
    18.     List <Vector3> vList = new List<Vector3>();
    19.     List <int>  triList = new List <int>();
    20.     List <Vector3> normalsList = new List<Vector3>();
    21.     List <Vector2> uvList = new List <Vector2>();
    22.  
    23.  
    24.  
    25. // Use this for initialization
    26. void Start () {
    27.  
    28. GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    29.  
    30.     vList.Add(new Vector3(0,0,0));
    31.     vList.Add(new Vector3(0,height,0));
    32.     vList.Add(new Vector3(0,0,0));
    33.     vList.Add(new Vector3(0,height,0));
    34.  
    35. Debug.Log("Started");
    36.  
    37.  
    38.     mesh.vertices = vList.ToArray();
    39.  
    40.  
    41. }
    42.  
    43. // Update is called once per frame
    44. void FixedUpdate () {
    45.  
    46. if(vList[z-1] != Wall.transform.position + new Vector3(0,0,0)
    47.     || vList[z] != Wall.transform.position + new Vector3(0,height,0))
    48. {
    49.     vList.Add(Wall.transform.position + new Vector3(0,0,0));
    50.     vList.Add(Wall.transform.position + new Vector3(0,height,0));
    51.     vList.Add(Wall.transform.position + new Vector3(0,0,0));
    52.     vList.Add(Wall.transform.position + new Vector3(0,height,0));
    53.  
    54.     mesh.vertices = vList.ToArray();
    55.     z += 4;
    56.     newcalc = true;
    57.  
    58.     Debug.Log("Noticed Vertices are not the same");
    59.  
    60. }
    61.  
    62. if(newcalc == true)
    63. {
    64.     triList.Add(i+0);
    65.     triList.Add(i+1);
    66.     triList.Add(i+4);
    67.  
    68.     triList.Add(i+1);
    69.     triList.Add(i+5);
    70.     triList.Add(i+4);
    71.  
    72.     triList.Add(i+6);
    73.     triList.Add(i+3);
    74.     triList.Add(i+2);
    75.  
    76.     triList.Add(i+6);
    77.     triList.Add(i+7);
    78.     triList.Add(i+3);
    79.  
    80.     mesh.triangles = triList.ToArray();
    81.     i = i+8;
    82.     newcalc = false;
    83.  
    84.  
    85.     normalsList.Add(new Vector3(0,0,1));
    86.     normalsList.Add(new Vector3(0,0,1));
    87.     normalsList.Add(new Vector3(0,0,1));
    88.     normalsList.Add(new Vector3(0,0,1));
    89.  
    90.     normalsList.Add(new Vector3(0,0,1));
    91.     normalsList.Add(new Vector3(0,0,1));
    92.     normalsList.Add(new Vector3(0,0,1));
    93.     normalsList.Add(new Vector3(0,0,1));
    94.  
    95.     mesh.normals = normalsList.ToArray();
    96. }
    97. }
    98. }
    I hope somebody can help me figure this out.
    Thanks in advance.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. vList.Add(Wall.transform.position + new Vector3(0,0,0));
    3. vList.Add(Wall.transform.position + new Vector3(0,height,0));
    4. vList.Add(Wall.transform.position + new Vector3(0,0,0));
    5. vList.Add(Wall.transform.position + new Vector3(0,height,0));
    6.  
    so vertex indexes 0, 1, 2, 3
    Code (csharp):
    1.  
    2. triList.Add(i+0);
    3. triList.Add(i+1);
    4. triList.Add(i+4);
    5.  
    build a triangle from 0, 1, 4 ... err, where did 4 come from?
     
    LordMikeVI likes this.
  3. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    In the Start function I generate my first 4 Vertices at line 30.
    Code (CSharp):
    1. void Start () {
    2. GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    3.     vList.Add(new Vector3(0,0,0));
    4.     vList.Add(new Vector3(0,height,0));
    5.     vList.Add(new Vector3(0,0,0));
    6.     vList.Add(new Vector3(0,height,0));
    7. Debug.Log("Started");
    8.     mesh.vertices = vList.ToArray();
    9. }
    Then in the FixedUpdate I generate 4 more at line 49.
    Code (CSharp):
    1. if(vList[z-1] != Wall.transform.position + new Vector3(0,0,0)
    2.     || vList[z] != Wall.transform.position + new Vector3(0,height,0))
    3. {
    4.     vList.Add(Wall.transform.position + new Vector3(0,0,0));
    5.     vList.Add(Wall.transform.position + new Vector3(0,height,0));
    6.     vList.Add(Wall.transform.position + new Vector3(0,0,0));
    7.     vList.Add(Wall.transform.position + new Vector3(0,height,0));
    8.     mesh.vertices = vList.ToArray();
    9.     z += 4;
    10.     newcalc = true;
    11.     Debug.Log("Noticed Vertices are not the same");
    12. }
    So there I have 8 Vertices.
    Does that explain your question or am I missing something?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    start only gets called once, so in the first instance of i you have 8 vertices, next iteration of i you have 8+4 vertices, and try to access 8+8
     
    LordMikeVI likes this.
  5. LordMikeVI

    LordMikeVI

    Joined:
    Jul 2, 2015
    Posts:
    15
    Thanks! That solved the problem!