Hi all,
I am trying to make the Enemy destroy the Player and the Enemy GameObject on contact, but currently, the OnCollision would only destroy the Enemy GameObject and not the Player.
Prefab Navigator (Tag “Player”)
Prefab Enemy (Tag “Enemy”)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerDead : MonoBehaviour {
public GameObject Player;
// Use this for initialization
void OnCollisionEnter(Collision EnemyCollide)
{
if (EnemyCollide.gameObject.tag == "Player")
{
// returns error:
// Destroying assets is not permitted to avoid data loss.
// If you really want to remove an asset use DestroyImmediate (theObject, true);
Destroy (this.gameObject);
Destroy (Player);
// returns error:
// Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks or OnValidate. You must use Destroy instead.
// UnityEngine.Object:smile:estroyImmediate(Object, Boolean)
// DestroyImmediate (this.gameObject, true);
// DestroyImmediate (Player, true);
}
}
}
Furthermore, upon destruction of the Enemy GameObject, I would receive either error in both cases. I really want to destroy both the Player and Enemy. Thanks.