Question by
MidnightCoffeeInc · Oct 14, 2018 at 11:44 PM ·
prefabsinstantiate prefabdestroy object
Destroying Instantiated Prefab in Another Script -- Can't destroy to prevent memory loss?
Hi,
So, basically, after an enemy dies I have it drop treasure for the player to pick up.
The problem is that when I try to destroy the spawned treasure (when it gets picked up), I get the error "Destroying assets is not permitted to avoid memory loss."
My code in the enemy script:
It basically chooses a random amount to spawn, then chooses a random prefab to spawn from an array.
void SpawnTreasure()
{
int treasureAmount;
if (armorObject) { treasureAmount = Random.Range(1, 6); }
else { treasureAmount = Random.Range(1, 4); }
for(int i = 0; i< treasureAmount; i++)
{
int index = Random.Range(1, Treasures.Length);
GameObject droppedTreasure = Treasures[index];
GameObject spawnedTreasures = (GameObject)Instantiate(droppedTreasure, transform.position, Quaternion.identity);
}
}
Then, In the script that's on the spawned treasure:
public void onGrab()
{
GetComponent<Rigidbody>().isKinematic = false;
collectSound.Play();
Destroy(gameObject, collectSound.clip.length);
Destroy(text, 2f);
}
I've tried everything mentioned in previous, similar posts, but I figured posting my exact situation would help!
Thanks so much!
Comment