destroyObject destroying the wrong object

Hey I’m new Unity and I have a problem destroying a specific object. I have a script that generates a bunch of cubes based off a cube prefab but when I try to destroy one of the cubes on a mouse click, it destroys the wrong block. It destroys one of the randomly generated blocks but not the one that was clicked on. The script is in the prefab cube that the generator works off. The script to destroy the blocks is in the prefab itself that the cube generator works from.

Here’s the Destroy script:

public class Mining : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnMouseDown()
    {
        Destroy(gameObject);   
    }
}

The scripts for the Cube generation are:

public class VoxelTools : MonoBehaviour
{

    private static GameObject cubePrefab;
    private static GameObject cubeContainer;
    private static int cubeCount = 0;
    private static List<GameObject> cubes;

    public static Color GetRandomColor()
    {
        float r = Random.Range(0f, 1f);
        float g = Random.Range(0f, 1f);
        float b = Random.Range(0f, 1f);

        //make grey/sludge colors less likely
        for (int i = 0; i < Random.Range(1, 3); i++)
        {
            if (Random.Range(0, 10) > 1)
            {
                int a = Random.Range(0, 3);
                if (a == 0)
                    r = 0;
                if (a == 1)
                    g = 0;
                if (a == 2)
                    b = 0;
            }
        }

        return new Color(r, g, b);
    }

    public static GameObject MakeCube(float x, float y, float z)
    {
        return MakeCube(x, y, z, Color.red, 1);
    }

    public static GameObject MakeCube(float x, float y, float z, Color color)
    {
        return MakeCube(x, y, z, color, 1);
    }

    public static GameObject MakeCube(float x, float y, float z, Color color, float size)
    {
        return MakeCube(new Vector3(x, y, z), color, size);
    }

    private static GameObject GetCubePrefab()
    {
        if (cubePrefab == null)
            cubePrefab = Resources.Load("Cube") as GameObject;
        return cubePrefab;
    }

    public static GameObject MakeCube(Vector3 position, Color color, float size)
    {
        cubeCount++;
        if (cubeContainer == null)
        {
            cubeContainer = new GameObject("cube container");
            cubes = new List<GameObject>();
        }

        GameObject cube = Instantiate(GetCubePrefab()) as GameObject;
        cubes.Add(cube);
        cube.transform.position = position;
        cube.transform.parent = cubeContainer.transform;
        cube.name = "cube " + cubeCount;

        cube.GetComponent<Renderer>().material.color = color;
        cube.transform.localScale = new Vector3(size, size, size);

        return cube;
    }

    public static void MakeAllCubesFall()
    {
        foreach (GameObject cube in cubes)
            if (cube.GetComponent<Rigidbody>() == null)
                cube.AddComponent<Rigidbody>();
    }
}

and:

public class VoxelSpheroid : MonoBehaviour {

	// Use this for initialization
	void Start ()
    {
        int radius = 6;
        Vector3 center = Vector3.zero;
        for (int i = -radius; i < radius; i++)
            for (int j = -radius; j < radius; j++)
                for (int k = -radius; k < radius; k++)
                {
                    Vector3 position = new Vector3(i, j, k);
                    float distance = Vector3.Distance(position, center);
                    if (distance < radius)
                        VoxelTools.MakeCube(i, j, k);
                }
	}
	
	// Update is called once per frame
	void Update ()
    {
    
	}
}

Any idea on how I’d do this so only the cube that was clicked on gets destroyed?

Thanks in advance

The function you posted is in a script in each cube or is in the generator?

Can you post the full script?

We need more info to help you :slight_smile:

EDIT: I just recreated a scene with the code you provided and it works flawlessly. But I found an issue: I had enabled Virtual Reality support in Player Settings. At first, it was destroying things randomly, but once I deactivated VR support it worked just fine.

If this is not your problem, try to recreate the issue in a new project or empty scene, and tell me if it’s still broken.