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;*
-
}*
}