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.