- Home /
Question by
Casas-Maestre · Jan 09, 2017 at 08:22 AM ·
randomgridinstantiate prefab
one prefab into random position of grid
I have one problem with instantiate 1 prefab on a random positions of grid, my code runs ok for instantiate but somtimes instantiate 2 prefabs in 1 position of grid, my code is: the rest of variables are correct but don't see here, and the grid is X10 Y21. some idea of the problem?
the code runs initial from other functions but call the void PlaceCubes() thanks.
public Shape[] block;
Transform[,] m_grid;
public static int numberOfCubes=0;
public static xmaxim = 10;
public static xminim = 0;
public static yminim = 0;
public static ymaxim = 3;
Vector3 GeneratedPosition()
{
int x,y,z;
x = Random.Range(xminim,xmaxim-1);
y = Random.Range(yminim,ymaxim);
z = 0;
posiciocalculada = new Vector3(x,y,z);
return new Vector3 (x, y, z);
}
void PlaceCubes()
{
rondes=numberOfCubes;
for (int i = 0; i < rondes; i++) {
Shape Blocknou = Instantiate (block [Random.Range (0, block.Length)], GeneratedPosition (), Quaternion.identity) as Shape;
if (GameObject.Find ("Shape1(Clone)").transform.position == posiciocalculada) {
Debug.Log ("Objecte trobat");
rondes = rondes + 1;
} else {
StoreShapeInGrid (Blocknou);
print (rondes);
print (" block generat " + Blocknou.transform.position);
}
}
}
public void StoreShapeInGrid(Shape shape)
{
if (shape == null)
{
return;
}
foreach (Transform child in shape.transform)
{
Vector2 pos = Vectorf.Round(child.position);
m_grid[(int) pos.x, (int) pos.y] = child;
}
}
Comment
Your answer
Follow this Question
Related Questions
random prefab instantiation 2 Answers
Generate closed, random path in C# 1 Answer