- Home /
How to put an Object in BackroundLayer whilst running the game,
Hey Guys, i wanna play an animation and destroy the animation after its end. For that i set a timer. My Problem is, that the GameObject is still existing the set time, although i want to remove the GameObject immediately after it was hit, i just want the explosion to play. Now my Idea was to put the Object in the BackroundLayer, so that you cant see and hit it anymore. I dont have any clue how to put an Object in the Backround while the Game is running. Would be nice if you could give me some Code for that... :) Thanks for ur answers.
void Die() { StartCoroutine(DestroyTimer());
}
IEnumerator DestroyTimer()
{
var cloneAnimation = Instantiate (deathEffect, transform.position, Quaternion.identity);
//At this point it want to change the Layer of the GameObject, so that the explosion can play
//but i cant see and shoot the GameObject anymore
yield return new WaitForSeconds(0.1f);
Destroy(this.gameObject);
Destroy (cloneAnimation);
}
you can access the sorting layer through the SpriteRenderer
SpriteRenderer sr;
void Method()
{
sr.sortingOrder=-100;
}
but I a better solution is to use a sound manager then you can destroy objects without having to worry about sounds being played. There are many good free ones on the asset store. It's also more performant.
Your answer
