- Home /
how to make my component disabled after an animation
i already done it my problem is the component is being disabled before the animation is done i have separate scripts by the way so here's the code
here's the first code this code is for the animation for my character
private void HandleAnswer(int answer) {
if (answer == 1)
{
//handle correct answer
ShowIncorrect = false;
ShowCorrect = true;
Poop = false;
animation.PlayQueued ("Wrong answer",QueueMode.PlayNow);
Enemy.animation.PlayQueued("ENMattck", QueueMode.CompleteOthers);
Attack();
NextQuestion();
}
else {
ShowIncorrect = true;
ShowCorrect = false;
Poop = false;
animation.PlayQueued ("Wrong answer",QueueMode.PlayNow);
Enemy.animation.PlayQueued("ENMattck", QueueMode.CompleteOthers);
AttackSelf();
}
here's the second code for my enemy which tells that if the enemy health is 0 the enemy will be disabled but what happens is after i push a certain button for the animation the enemy is disabled so i need a timer or something so that until the animation is done my component wont be disabled
void Update () {
if (curHealth <= 0) {
PlayerPrefs.SetInt("SavedLevel1", 1);
Debug.Log("SavedLevel = 1");
Enemy.gameObject.SetActive(false);
}
Answer by RedDevil · Feb 15, 2014 at 08:16 AM
A timer would be
yield return new WaitForSeconds(amountoftime); But if you do this you would have to declare the Update function as an IENumerator and this is not allowed. I sugest you do it like this:
if (curHealth <= 0) {
StartCoroutine(ObjectActive());
}
IENumerator ObjectActive() {
PlayerPrefs.SetInt("SavedLevel1",1);
Debug.Log("SavedLevel = 1");
yield return new WaitForSeconds(2);
Enemy.gameObject.SetActive(false);
}
thank you very much you really help me.
by the way its not part of the topic but can you help me with this one its also about animation what happens is after my enemy dies, a first animation should play witch is the attack animation and the second one is after that animation so here's the code
void Update () {
if(!Enemy.animation.IsPlaying("EN$$anonymous$$attck")){
if(!animation.IsPlaying("Wrong answer")){
if (EH.curHealth <= 0) {
Poop=false;
ShowCorrect = false;
ShowIncorrect = false;
animation.Play("Next_Enemy");
}
so i did it but what happens is after my first animation play's the second one plays but the second one will keeps on repeating itself, can you help me ? but if not, its okay its not part of my question anyway thankyou
you should check on the animation if its set to loop.If its not then its because the Cur Health if constantly getting called since you got no more Health.
nope its not, i think its because i put it on update function but if i dont put it there it wont work, is there anything else i can do...
if this animation is for the enemy that will die then you can just destroy the game object after he played the animation
if you destroyed the gameobject errors will occur thats why i just disabled the components.