Ive been working on this auto generated map from a while but the problem im facing is that the “Ground” and “Block” objects are being double created.
This script works like this (about Blocks) : When i start the game the script is set to create duplicates of Block prefab,There is meant to be one block in each layer,a block on random ‘X’ pos from (3, 0 -3) and each block is in a distance of 10 Z,but this script is acting weird as it creates 2 blocks in random X positions instead of one (like 1st block’s pos = 3 and second block’s pos = 0,or even 2 blocks in one place).
It seems like the script is running twice but ive checked this script on other objects but i cant find this script attached to any other object.
Here is the script:
public class PCDetector : MonoBehaviour {
public Transform Block;
private GameObject Ground;
private float GroundDistance = 100;
private float BlockX;
private float BlockZ;
public float BlockDistance;
private int Value;
private int OldValue;
void Awake () {
CreateBoxes (150.0f);
}
void OnCollisionEnter (Collision collision)
{
if (collision.collider.CompareTag ("PClone") && gameObject.CompareTag ("GClone")) {
gameObject.tag = "Ground";
Ground = Instantiate (gameObject, new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + GroundDistance), Quaternion.identity);
Ground.tag = "GClone";
Destroy (Ground, 10);
Destroy (Ground = Instantiate (gameObject, new Vector3 (Ground.transform.position.x, Ground.transform.position.y, Ground.transform.position.z + GroundDistance), Quaternion.identity), 10);
CreateBoxes (Ground.transform.position.z);
}
}
public void CreateBoxes (float BlockLimit) {
while (BlockZ < BlockLimit) {
RandomBlockX ();
BlockZ = BlockZ + BlockDistance;
Instantiate (Block, new Vector3 (BlockX, 2.0f, BlockZ), Quaternion.identity);
}
}
void RandomBlockX () {
while (Value == OldValue) {
Value = Random.Range (0, 3);
}
switch (Value) {
default:
{
BlockX = 3.0f;
break;
}
case 0:
{
BlockX = 0.0f;
break;
}
case 1:
{
BlockX = -3.0f;
break;
}
}
OldValue = Value;
}
}