I'm working on a level in a 2D game that includes two big triggers.
Each time the player enters them, I Instantiate
an enemy prefab. Then, the player can shoot bullets to destroy that prefab. And finally, the enemy should die by playing an animation, disabling his collider, and destroying itself.
However, the problem is that every time a bullet hits an enemy, I get the warning "Animator is not playing an AnimatorController" and the error "Destroying assets is not permitted to avoid data loss." in the console. And these two prevent the enemy clones from getting destroyed.
The warning is caused by the line enemySpawnPoint1.destroyPurpleGoon.GetComponent<Animator>().SetTrigger("dead");
and the error is caused by the line Destroy(enemySpawnPoint1.destroyPurpleGoon, 4f);
.
This is the script for the first trigger:
using UnityEngine;
public class EnemySpawnPoint1 : MonoBehaviour
{
[HideInInspector] public int enemyCount = 0;
// I assigned the enemy prefab to the Inspector.
public GameObject purpleGoon;
public GameObject destroyPurpleGoon;
private void Awake()
{
// I wrote this because for some reason, instantiated enemies fall when the game begins.
purpleGoon.GetComponent<CapsuleCollider2D>().enabled = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject == Player.Instance.gameObject && enemyCount < 20)
{
InstantiateEnemy1();
}
}
void InstantiateEnemy1()
{
destroyPurpleGoon = Instantiate(purpleGoon, new Vector3(65.46f, -1.11f, 0), Quaternion.identity);
enemyCount++;
}
}
And this is the script for the second trigger:
using UnityEngine;
public class EnemySpawnPoint2 : MonoBehaviour {
public EnemySpawnPoint1 enemySpawnPoint1;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject == Player.Instance.gameObject && enemySpawnPoint1.enemyCount < 20)
{
InstantiateEnemy2();
}
}
void InstantiateEnemy2()
{
enemySpawnPoint1.destroyPurpleGoon = Instantiate(enemySpawnPoint1.purpleGoon, new Vector3(-1.309999f,
-1.11f, 0), Quaternion.identity);
enemySpawnPoint1.enemyCount++;
}
}
I write and call DestroyInstantiatedEnemy()
function in ShotgunBullet
script:
public EnemySpawnPoint1 enemySpawnPoint1;
public void DestroyInstantiatedEnemy()
{
enemySpawnPoint1.destroyPurpleGoon.GetComponent<Animator>().SetTrigger("dead");
enemySpawnPoint1.destroyPurpleGoon.GetComponent<CapsuleCollider2D>().enabled = false;
Destroy(enemySpawnPoint1.destroyPurpleGoon, 4f);
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
DestroyInstantiatedEnemy();
Destroy(gameObject);
}
}
The warning is strange to me because the animator components on the original prefab and all the prefab clones have the AnimatorController
assigned to them in their Animator components. Even the idle animation plays perfectly fine in the runtime, which shows the Animator component works.
About the error, ideally I should use an empty game object and destroy that instead. But since I destroy the enemy prefabs in another script, I had to make it public. So now, Unity thinks I want to destroy the actual prefab and not the clones. And I can't figure out how to fix it.
I also noticed that sometimes the collider gets disabled on the original prefab (the one in Project window) and dies in Awake. So, I wrote a line of code to keep it enabled from the beginning. This and the fact that I get that error prove that I'm directly making changes to the root prefab instead of the clones. But I don't know why.
It is worth noting that in the other levels, the player uses a knife to kill the enemy prefabs (I directly put those prefabs in the scene without instantiating them) and their death function (which is the same as DestroyInstantiatedEnemy()
but without the references to destroyPurpleGoon
) works perfectly fine. This means that this warning only occurs in this specific scene where they are instantiated in runtime and can only die by bullets.
I researched a lot about this problem and found some similar questions with solutions on Unity forums; but none of them have worked for me. Additionally, as mentioned in the comments below, I'm aware that the way I use destroyPurpleGoon
is problematic but I don't know a better way to use that variable.
I would appreciate it if you tell me the reason behind the warning and the error and how I should fix them.
DestroyInstantiatedEnemy
know which of yourdestroyPurpleGoon
should be destroyed?InstantiateEnemy1
is always only saving the latest createddestroyPurpleGoon
, a previous assigned one is getting overwritten \$\endgroup\$