Im using assets that dont have a collider built in and when the enemy is destroyed there are children who arent destroyed with it. For my Player helicopter I used the following code `[SerializeField] GameObject meshChildren;
void OnTriggerEnter(Collider other)
{
StartCrashSequence();
}
private void StartCrashSequence()
{
GetComponent<BoxCollider>().enabled = false;
GetComponent<MeshRenderer>().enabled = false;
HideMeshInChildren();
crashParticles.Play();
GetComponent<PlayerController>().enabled = false;
Invoke("ReloadLevel", crashDelay);
}
void ReloadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
void HideMeshInChildren()
{
foreach (GameObject part in meshChildren)
{
MeshRenderer mesh = part.GetComponentInChildren<MeshRenderer>();
mesh.enabled = false;
}
}`
which fixed the issue. When trying to use the same method on my enemy script its not having the same outcome and the children are remaining. Obviosly I didnt need the reload cutscene part
My code for enemy looks like this `[SerializeField] GameObject meshChildren;
Scoreboard scoreBoard;
void Start()
{
scoreBoard = FindObjectOfType<Scoreboard>();
AddRigidBody();
}
void AddRigidBody()
{
Rigidbody rb = gameObject.AddComponent<Rigidbody>();
rb.useGravity = false;
}
void OnParticleCollision(GameObject other)
{
ProcessHit();
if (hitPoints < 1)
{
KillEnemy();
}
}
private void ProcessHit()
{
hitPoints --;
scoreBoard.IncreaseScore(scorePerHit);
GameObject vfx = Instantiate(hitVFX, transform.position, Quaternion.identity);
vfx.transform.parent = parent;
}
private void KillEnemy()
{ //Instantiates explosion VFX in the location of the enemy and hides the mesh renderer of the main body and its children.
GameObject vfx = Instantiate(deathVFX, transform.position, Quaternion.identity);
vfx.transform.parent = parent;
GetComponent<MeshRenderer>().enabled = false;
GetComponent<CapsuleCollider>().enabled = false;
HideMeshInChildren();
}
void HideMeshInChildren()
{
foreach (GameObject part in meshChildren)
{
MeshRenderer mesh = part.GetComponentInChildren<MeshRenderer>();
mesh.enabled = false;
}
}
}`
I commented out the add rigid body to test if that was the issue but it made no difference