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