Need Help with cloning and destroying

Hey Folks,

I could use some help with my code. I am trying to destroy an array of objects that spawn on screen, but I keep getting the error “Destroying assets is not permitted to avoid data loss.” I understand that I’m doing something wrong and trying to destroy the prefab instead of the clone, I just need help actually finding what that is.

    private float timer = 0;
    public GameObject[] fruits;
    public float spawnRate = 5;
    public float heightOffset = 10;
    public float widthOffset = 20;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (timer < spawnRate) {
            timer += Time.deltaTime;
        }
        else {
        spawnFruits();
        timer = 0;
        }

        if (Input.GetKeyDown(KeyCode.Mouse0)) {
            Destroy(fruits[0]);
        }
    }

    public void spawnFruits() {
        float lowestPoint = transform.position.y - heightOffset;
        float highestPoint = transform.position.y + heightOffset;
        float leftPoint = transform.position.x - widthOffset;
        float rightPoint = transform.position.x + widthOffset;
        int randomIndex = Random.Range(0, fruits.Length);

        Instantiate(fruits[randomIndex], new Vector3(Random.Range(leftPoint, rightPoint), Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    }
}

Thank you anyone for the help

Your code is trying to destroy the prefabs in your project files, as opposed to the instances in the scene.

Instantiate returns the copies which is what you should be destroying, though in this instances you probably want to actually use a raycast to check if you’re clicking on a fruit, and use the information gleamed from the raycast to get the game object to destroy.

Okay I got my real answer. I created a new script called “DestroyFruit” and added the simple code at the bottom of this reply. I then attached that script onto each one of my Prefab fruits.

public class DestroyFruit : MonoBehaviour
{
    void OnMouseDown()
    {
        Destroy(gameObject);
    }
}