I'm trying to make rubik's cube game using unity and C#
.
But i have problem when i want to add an item into a list.
The problem is that the same item is constantly added to the list, I tried to use Contains()
function but it doesn't work.
List<node>[,,] _NodeLi;
Note: I need to use this on the Update()
method.
void Update()
{
nodes = new node[3, 3, 3];
Vector3 cubeButtonLeftDown = new Vector3((size / 2 * -1) + (size / 3), (size / 2 * -1) + (size / 3)
, (size / 2 * -1) + (size / 3));
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
for (int z = -1; z <= 1; z++)
{
node Node = new node(new Vector3(x, y, z), new Vector3
(x * size / 6,
y * size / 6,
z * size / 6));
nodes[x + 1, y + 1, z + 1] = Node;
}
}
}
// find the faces of the cube
//find the y faces
foreach(node Node_ in nodes) {
if (Node_.InRubikCubePos.y == 1)
{
if (buttonYFaces.Contains(Node_))
buttonYFaces.Remove(Node_);
if (!topYFaces.Contains(Node_))
topYFaces.Add(Node_);
}
else if (Node_.InRubikCubePos.y == -1)
{
if (topYFaces.Contains(Node_))
topYFaces.Remove(Node_);
if (!buttonYFaces.Contains(Node_))
buttonYFaces.Add(Node_);
}
else
{
if (buttonYFaces.Contains(Node_))
buttonYFaces.Remove(Node_);
if (topYFaces.Contains(Node_))
topYFaces.Remove(Node_);
}
}
// find the x faces
foreach (node Node_ in nodes)
{
if (Node_.InRubikCubePos.x == 1)
{
if (leftXFaces.Contains(Node_))
leftXFaces.Remove(Node_);
if (!rightXFaces.Contains(Node_))
rightXFaces.Add(Node_);
}
else if (Node_.InRubikCubePos.x == -1)
{
if (rightXFaces.Contains(Node_))
rightXFaces.Remove(Node_);
if (!leftXFaces.Contains(Node_))
leftXFaces.Add(Node_);
}
else
{
if (leftXFaces.Contains(Node_))
leftXFaces.Remove(Node_);
if (rightXFaces.Contains(Node_))
rightXFaces.Remove(Node_);
}
}
// find the z faces
foreach (node Node_ in nodes)
{
if (Node_.InRubikCubePos.z == 1)
{
if (leftZFaces.Contains(Node_))
leftZFaces.Remove(Node_);
if (!rightZFaces.Contains(Node_))
rightZFaces.Add(Node_);
}
else if (Node_.InRubikCubePos.z == -1)
{
if (rightZFaces.Contains(Node_))
rightZFaces.Remove(Node_);
if(!leftZFaces.Contains(Node_))
leftZFaces.Add(Node_);
}
else
{
if (leftZFaces.Contains(Node_))
leftZFaces.Remove(Node_);
if (rightZFaces.Contains(Node_))
rightZFaces.Remove(Node_);
}
}
// find the vert positions
if (vertPositions.Count < 8)
{
foreach (float x in _One)
{
foreach (float y in _One)
{
foreach (float z in _One)
{
vertPositions.Add(new Vector3(x, y, z));
}
}
}
}
foreach(Vector3 n in vertPositions)
{
Debug.Log(n);
}
Debug.Log(vertPositions.Count);
Debug.Log("Y :" + topYFaces.Count +",,,,, :" +buttonYFaces.Count);
}
private void OnDrawGizmos()
{
if (nodes != null)
{
foreach (node n in nodes)
{
Gizmos.DrawWireCube(n.inWorldPos, Vector3.one * (size / 6));
}
}
}
And this is the node class:
public class node{
public Vector3 InRubikCubePos;
public Vector3 inWorldPos;
//IRCPos mean's " InRubikCubePos " and the IWPos mean's " inWorldPos "
public node(Vector3 IRCPos, Vector3 IWPos)
{
InRubikCubePos = IRCPos;
inWorldPos = IWPos;
}
}