Hide mesh renderer in children

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

I did not do this course, but I am curious as to why are you hiding the enemy and not destroying the game object? If I look at the course code, Rick is destroying the enemy. Hiding the mesh renderers still keeps the object around so it will continue to process, even if it’s not visible anymore.

You know what, that makes so much more sense to do that. I went that way on my player character because it would destroy the script attached to it also but thats obviously not an issue for the enemy. Thanks mate.

There is one problem that you may face if you destroy the enemy. You are attaching the vfx to the enemy. If you destroy the object, the vfx will be destroyed as well and you won’t see the explosion. You should be able to solve this by just not parenting the vfx to the enemy.

The vfx is parented to a seperate game object and instantiated on the location of the enemy when the method is called. But if the destroy method is called after the VFX method is called it should play and not have an impact on the explosion. Does that sound right to you?

I see the vfx is attached to the enemy’s parent. That should be fine, although that parent will also stick around. You can have the explosion destroy itself when it’s done by setting the Stop Action on the particle system
image

Privacy & Terms