Double Objects Being Created

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;

    }
}

You’re calling Instantiate in 3 places in this script, are you sure that is not the root of your issue?

The first 2 Instantiate are for creating 2 Ground objects and the 3rd one is used in a loop which is for creating block objects in a number of layers.

So…when you put a debug.log in awake, it’s printing to the console twice? Or in CreateBoxes?

I’m not following the logic here. Every time OnCollisionEnter gets called, I would expect 2 or more blocks to spawn.

void OnCollisionEnter(Collision collision) {
    if(collision.collider.CompareTag("PClone") && gameObject.CompareTag("GClone")) {

        gameObject.tag = "Ground";

        // create BLOCK1 at this object's position + some z distance
        Ground = Instantiate(gameObject, new Vector3(transform.position.x, transform.position.y, transform.position.z + GroundDistance), Quaternion.identity);
        Ground.tag = "GClone";
         
        Destroy(Ground, 10);

        // create BLOCK2 at the BLOCK1's position + some z distance
        Destroy(Ground = Instantiate(gameObject, new Vector3(Ground.transform.position.x, Ground.transform.position.y, Ground.transform.position.z + GroundDistance), Quaternion.identity), 10);

        // create more blocks?
        CreateBoxes(Ground.transform.position.z);
    }
}

@jrffrtrryschoch the 2 Instantiate funtions are used for creating the 2 ‘Floor’ objects and the 'CreateBoxes" is used to create a number of possible blocks/boxes from point A to point B with the distance of 10-Z each block.

I just separately tested the Awake () and CreateBoxes () function and YES,they are bing called 2 times!

If Awake is being called twice, that would suggest you have two copies of the script or you are creating an additional copy at the start(even destroying and recreating perhaps). Awake is always called just once, so it shouldn’t be running twice on it’s own. I know you said you checked, but I would double check that you don’t have two copies of the script in your scene during play mode as well as when you’re editing. If you do see it on one gameobject, make sure you don’t have two copies of the script on the same gameobject as well.

You could even just add a Debug.Log(gameObject.name); If you need to to the Awake.

You can actually right click on your script and there’s an option there for seeing what objects it’s attached to. I forget what the menu item text is though. Probably something about references though. But that gives a real easy/fast way to find out.

Ah, yeah. I know what you referring to. I use that for sprites sometimes. I’m use to just typing scripts into the hierarchy search, so didn’t think of using it for that.

Debug.Log("message", gameObject);

will highlight the object in the scene when clicking the message in the console.