Hello, i am new here and i hope that somebody can help me with my problem.
I am trying to create a script, which creates a mesh out of information from a textfile in a voxel design (the voxeleditor i use saves its models in a simple text format ^^)
For this task i did create my own Variabletype I called “Voxel”, which simple saves a color as Vector3 and information about the shown faces/sides as a int-array (simply: is a side drawn (=1) or not drawn (=0)).
Now i do have the problem that, when ever i set one face of one Voxelinstance to 1, all other instances of Voxel also get that value changed to 1.
In the beginning i used “struct” and not “class” for the “Voxel”-Script, but i changed it just for checking if that was the fault.
I hope i described everything cleary enough and that somebody has got a simple solution for me.
Greetings, Firedra.
Voxel class:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Voxel{
private Vector3 color;
public int[] face;
public Voxel (Vector3 c, int[] f)
{
color = new Vector3();
face = new int[6];
if (f.Length == 6){
color = c;
face = f;
}
}
public void SetColor(Vector3 newColor)
{
color = newColor;
}
// Faceindendifier
// 0 = Front
// 1 = Back
// 2 = Down
// 3 = Up
// 4 = Left
// 5 = Right
public void SetFace(int index)
{
this.face[index] = 1;
}
public void RemoveFace(int index)
{
this.face[index] = 0;
}
public Vector3 GetColor()
{
return color;
}
public int GetFace(int index)
{
return face[index];
}
}
Test Script to show the state of a face via GUI for 6 Voxelinstances.
using UnityEngine;
using System.Collections;
public class _DebugVoxelFace : MonoBehaviour {
public Voxel[] blocks;
// Use this for initialization
void Start () {
blocks = new Voxel[6];
int[] f = new int[6];
f[0] = 0;
f[1] = 0;
f[2] = 0;
f[3] = 0;
f[4] = 0;
f[5] = 0;
blocks[0] = new Voxel(new Vector3(1,0,0),f);
blocks[1] = new Voxel(new Vector3(2,0,0),f);
blocks[2] = new Voxel(new Vector3(3,0,0),f);
blocks[3] = new Voxel(new Vector3(4,0,0),f);
blocks[4] = new Voxel(new Vector3(5,0,0),f);
blocks[5] = new Voxel(new Vector3(6,0,0),f);
}
void OnGUI(){
for(int i = 0; i < 6; i++){
string s = blocks*.GetFace(0).ToString();*
_ if (GUI.Button(new Rect(200+80i,200,70,50),s)){_
if (blocks_.GetFace(0)==0) blocks.SetFace(0);
else blocks.RemoveFace(0);
}
}
}
}*_