Destroy a Prefab from an Array? (C#)

Hey you, yeah you my friend! I have a problem with my (C#) code, I’m trying to delete a random prefab created by an array. I instantiate a random prefab and it spawns out in the scene, the prefab is going to stay in the scene for four seconds, it will then destroy itself. This is where the problem shows up! I try destroying the prefab with help of a loop, but get the error “Destroying assets is not permitted to avoid data loss

How can I make the prefab destroy itself? Grateful for any help I can get! Thank you! Tried searching for the problem but most of it doesn’t work or is in Java script.

using UnityEngine;
using System.Collections;

public class SpawnPowerUp : MonoBehaviour 
{
	// Power up array
	public GameObject[] PowerUpsPrefabs;
	
	// Borders
	public Transform borderTop;
	public Transform borderBottom;
	public Transform borderLeft;
	public Transform borderRight;

	//Timer
	public float timer = 4.0f; 

	//HasSpawned
	public bool hasPowerUpSpawned = false;

	void Start () 
	{
		// Spawn a new power up every 10 seconds, starting in 10
		InvokeRepeating("Spawn", 10, 10);
	}

	void Update()
	{
		if(hasPowerUpSpawned == true)
		{
			timer -= Time.deltaTime;
		}

		if(timer <= 0.0f)
		{
			for (int i= 0; i< PowerUpsPrefabs.Length; i++)
			{
				Destroy (PowerUpsPrefabs_.gameObject); ***//Error here***_
  •  	}*
    
  •  }*
    
  • }*

  • // Spawn a Power up*

  • void Spawn()*

  • {*

  •  //Random range between every power up*
    
  •  int index = Random.Range (0, PowerUpsPrefabs.Length);*
    
  •  // x position between left & right border*
    
  •  int x = (int)Random.Range(borderLeft.position.x,*
    
  •                            borderRight.position.x);*
    
  •  // y position between top & bottom border*
    
  •  int y = (int)Random.Range(borderBottom.position.y,*
    
  •                            borderTop.position.y);*
    
  •  // Instantiate the power up at (x, y)*
    
  •  Instantiate(PowerUpsPrefabs[index],*
    
  •              new Vector2(x, y),*
    
  •              Quaternion.identity); // default rotation*
    
  •  hasPowerUpSpawned = true;*
    
  • }*

}

You actually dont want to destroy the prefab. you want to destroy the instance (note the difference here). As the prefab is located in the project folder, and destroying it would permanently destroy it, Unity realises that this is probably not what you want and throws an error. The instance (the thing you create by calling Instantiate with the prefab as parameter) is the thing you see in your scene/hierarchy, which is a copy of the prefab and which is the thing that you want to destroy. If you want a reference to the instance, Instantiate returns the instance you create so you could use that…

The quick solution here would just be changing line 59 to:

Destroy(Instantiate(PowerUpsPrefabs[index],
                     new Vector2(x, y),
                     Quaternion.identity), 4f); // Creates an instance and destroyes it after 4 seconds

And probaby getting rid of the Update method entirely as the Object now gets destroyed in another way.

But then It would probably be better to give your prefab a script that destroys itself as Ali hatem suggested, as you can have more control over the conditions of when the object should get destroyed without having to create a list of all your instances.

looks like you are trying to destroy your assets you’ve stored in PowerUpsPrefabs. Instead you should have a reference to the instantiated PowerUpsPrefabs inside the scene and destroy them.

You could e.g. when you instantiate yout Prefabs, save this references to a new array, iterate over this new array to destroy them.