Problem with generating a mesh behind an Object

Hello,
I have a problem with generating a wall behind a moving object by scripting it.
The Errors are here:

The Code is as following in C#

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]

public class WallScript : MonoBehaviour {


    public GameObject Wall;
    public float height = 1;
    private int z = 1;
    private int i = 0;
    private bool newcalc = false;
    private Mesh mesh;


    List <Vector3> vList = new List<Vector3>();
    List <int>  triList = new List <int>();
    List <Vector3> normalsList = new List<Vector3>();
    List <Vector2> uvList = new List <Vector2>();



// Use this for initialization
void Start () {

GetComponent<MeshFilter>().mesh = mesh = new Mesh();

    vList.Add(new Vector3(0,0,0));
    vList.Add(new Vector3(0,height,0));
    vList.Add(new Vector3(0,0,0));
    vList.Add(new Vector3(0,height,0));

Debug.Log("Started");


    mesh.vertices = vList.ToArray();


}

// Update is called once per frame
void FixedUpdate () {

if(vList[z-1] != Wall.transform.position + new Vector3(0,0,0)
    || vList[z] != Wall.transform.position + new Vector3(0,height,0))
{
    vList.Add(Wall.transform.position + new Vector3(0,0,0));
    vList.Add(Wall.transform.position + new Vector3(0,height,0));
    vList.Add(Wall.transform.position + new Vector3(0,0,0));
    vList.Add(Wall.transform.position + new Vector3(0,height,0));

    mesh.vertices = vList.ToArray();
    z += 4;
    newcalc = true;

    Debug.Log("Noticed Vertices are not the same");

}

if(newcalc == true)
{
    triList.Add(i+0);
    triList.Add(i+1);
    triList.Add(i+4);

    triList.Add(i+1);
    triList.Add(i+5);
    triList.Add(i+4);

    triList.Add(i+6);
    triList.Add(i+3);
    triList.Add(i+2);

    triList.Add(i+6);
    triList.Add(i+7);
    triList.Add(i+3);

    mesh.triangles = triList.ToArray();
    i = i+8;
    newcalc = false;


    normalsList.Add(new Vector3(0,0,1));
    normalsList.Add(new Vector3(0,0,1));
    normalsList.Add(new Vector3(0,0,1));
    normalsList.Add(new Vector3(0,0,1));

    normalsList.Add(new Vector3(0,0,1));
    normalsList.Add(new Vector3(0,0,1));
    normalsList.Add(new Vector3(0,0,1));
    normalsList.Add(new Vector3(0,0,1));

    mesh.normals = normalsList.ToArray();
}
}
}

I hope somebody can help me figure this out.
Thanks in advance.

vList.Add(Wall.transform.position + new Vector3(0,0,0));
vList.Add(Wall.transform.position + new Vector3(0,height,0));
vList.Add(Wall.transform.position + new Vector3(0,0,0));
vList.Add(Wall.transform.position + new Vector3(0,height,0));

so vertex indexes 0, 1, 2, 3

triList.Add(i+0);
triList.Add(i+1);
triList.Add(i+4);

build a triangle from 0, 1, 4 … err, where did 4 come from?

1 Like

In the Start function I generate my first 4 Vertices at line 30.

void Start () {
GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    vList.Add(new Vector3(0,0,0));
    vList.Add(new Vector3(0,height,0));
    vList.Add(new Vector3(0,0,0));
    vList.Add(new Vector3(0,height,0));
Debug.Log("Started");
    mesh.vertices = vList.ToArray();
}

Then in the FixedUpdate I generate 4 more at line 49.

if(vList[z-1] != Wall.transform.position + new Vector3(0,0,0)
    || vList[z] != Wall.transform.position + new Vector3(0,height,0))
{
    vList.Add(Wall.transform.position + new Vector3(0,0,0));
    vList.Add(Wall.transform.position + new Vector3(0,height,0));
    vList.Add(Wall.transform.position + new Vector3(0,0,0));
    vList.Add(Wall.transform.position + new Vector3(0,height,0));
    mesh.vertices = vList.ToArray();
    z += 4;
    newcalc = true;
    Debug.Log("Noticed Vertices are not the same");
}

So there I have 8 Vertices.
Does that explain your question or am I missing something?

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

1 Like

Thanks! That solved the problem!