Home Game Development unity – How can I remedy this error and this warning when destroying instantiated prefabs?

unity – How can I remedy this error and this warning when destroying instantiated prefabs?

0
unity – How can I remedy this error and this warning when destroying instantiated prefabs?

[ad_1]

I am engaged on a stage in a 2D recreation that features two massive triggers.

Every time the participant enters them, I Instantiate an enemy prefab. Then, the participant can shoot bullets to destroy that prefab. And at last, the enemy ought to die by taking part in an animation, disabling his collider, and destroying itself.

Nonetheless, the issue is that each time a bullet hits an enemy, I get the warning “Animator shouldn’t be taking part in an AnimatorController” and the error “Destroying property shouldn’t be permitted to keep away from knowledge loss.” within the console. And these two stop the enemy clones from getting destroyed.
The warning is brought on by the road enemySpawnPoint1.destroyPurpleGoon.GetComponent<Animator>().SetTrigger("useless"); and the error is brought on by the road Destroy(enemySpawnPoint1.destroyPurpleGoon, 4f);.

That is the script for the primary set off:

utilizing UnityEngine;

public class EnemySpawnPoint1 : MonoBehaviour
{
    [HideInInspector] public int enemyCount = 0;

    // I assigned the enemy prefab to the Inspector.
    public GameObject purpleGoon;
    public GameObject destroyPurpleGoon;


    non-public void Awake()
    {
       // I wrote this as a result of for some cause, instantiated enemies fall when the sport begins. 
       purpleGoon.GetComponent<CapsuleCollider2D>().enabled = true;
    }

    non-public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject == Participant.Occasion.gameObject && enemyCount < 20)
        {
            InstantiateEnemy1();
        }
    }

    void InstantiateEnemy1()
    {
        destroyPurpleGoon = Instantiate(purpleGoon, new Vector3(65.46f, -1.11f, 0), Quaternion.identification);
        enemyCount++;
    }
}

And that is the script for the second set off:

utilizing UnityEngine;

public class EnemySpawnPoint2 : MonoBehaviour {

    public EnemySpawnPoint1 enemySpawnPoint1;


    non-public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject == Participant.Occasion.gameObject && enemySpawnPoint1.enemyCount < 20)
        {
            InstantiateEnemy2();
        }
    }

    void InstantiateEnemy2()
    {
        enemySpawnPoint1.destroyPurpleGoon = Instantiate(enemySpawnPoint1.purpleGoon, new Vector3(-1.309999f,
-1.11f, 0), Quaternion.identification);
        enemySpawnPoint1.enemyCount++;
    } 
}

I write and name DestroyInstantiatedEnemy() operate in ShotgunBullet script:

public EnemySpawnPoint1 enemySpawnPoint1;


public void DestroyInstantiatedEnemy()
{
    enemySpawnPoint1.destroyPurpleGoon.GetComponent<Animator>().SetTrigger("useless");
    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 unusual to me as a result of the animator parts on the unique prefab and all of the prefab clones have the AnimatorController assigned to them of their Animator parts. Even the idle animation performs completely high-quality within the runtime, which exhibits the Animator element works.

In regards to the error, ideally I ought to use an empty recreation object and destroy that as an alternative. However since I destroy the enemy prefabs in one other script, I needed to make it public. So now, Unity thinks I need to destroy the precise prefab and never the clones. And I can not work out learn how to repair it.

I additionally observed that typically the collider will get disabled on the unique prefab (the one in Venture window) and dies in Awake. So, I wrote a line of code to maintain it enabled from the start.
This and the truth that I get that error show that I am straight making adjustments to the basis prefab as an alternative of the clones. However I do not know why.

It’s value noting that within the different ranges, the participant makes use of a knife to kill the enemy prefabs (I straight put these prefabs within the scene with out instantiating them) and their loss of life operate (which is identical as DestroyInstantiatedEnemy() however with out the references to destroyPurpleGoon) works completely high-quality. Which means this warning solely happens on this particular scene the place they’re instantiated in runtime and may solely die by bullets.

I researched loads about this drawback and located some related questions with options on Unity boards; however none of them have labored for me. Moreover, as talked about within the feedback beneath, I am conscious that the way in which I take advantage of destroyPurpleGoon is problematic however I do not know a greater method to make use of that variable.

I might admire it in case you inform me the rationale behind the warning and the error and the way I ought to repair them.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here