- Home /
All Arrayinstances get changed
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[i].GetFace(0).ToString();
if (GUI.Button(new Rect(200+80*i,200,70,50),s)){
if (blocks[i].GetFace(0)==0) blocks[i].SetFace(0);
else blocks[i].RemoveFace(0);
}
}
}
}
Answer by NoseKills · Mar 03, 2015 at 11:09 AM
All your Voxel instances have a reference to the same array int[] f = new int[6];
that you create in _DebugVoxelFace.Start()
. Whatever you put into that array is accessed by all Voxels.
You could just do
blocks[0] = new Voxel(new Vector3(1,0,0), new int[6]);
blocks[1] = new Voxel(new Vector3(2,0,0), new int[6]);
blocks[2] = new Voxel(new Vector3(3,0,0), new int[6]);
blocks[3] = new Voxel(new Vector3(4,0,0), new int[6]);
blocks[4] = new Voxel(new Vector3(5,0,0), new int[6]);
blocks[5] = new Voxel(new Vector3(6,0,0), new int[6]);
Thank you very much! I allready knew it would be something little... ^^ But i didnt think that "f" would be such a trouble because i just copied the values of, atleast that is what i thought ^^. Have a nice day, sir!
Your answer
Follow this Question
Related Questions
Struct for holding Class Parameters 0 Answers
How -exactly- do classes and structs work in Unity? 0 Answers
Conversion of a static class to struct? 1 Answer
more Burgzerg Blues 1 Answer
Instancing class automatically? 2 Answers