- Home /
Animation Event issue what am I doing wrong here
OK now this is kind of strange. I had been working all weekend trying to figure this out, I eventually got my UFO to take off from the hilltop stone circle as my FPS player gets within a certain range and I've even managed to have the UFO destroy itself with and animation event at the end of the clip and replace the UFO with a dead replacement complete with a Ragdoll dead Alien Zombie that slumps over nicely in the seat when the UFO dead replacement "Instantiates" got some fire happnin too with a nice cracking fire sound. I still have not been able to figure out how to get an explosion to occure about midway through the UFO's fight path though :(
The code works but I get a script error that says: "Update() can not be a coroutine" Whatever that means??
Here is my code that I've attached to my UFO that the animation event calls upon if you would like to have a look.
var UFOexplosionSound : AudioClip; var explosion : GameObject; var wreck : GameObject; function ExplodeUFOsound (clip : AudioClip)
{ audio.PlayOneShot(UFOexplosionSound); }
function Update () { yield WaitForSeconds(3); KillSelf(); }
function KillSelf () { var wreckClone = Instantiate(wreck, transform.position, transform.rotation); Destroy(gameObject); }
Like I say it appears to work only I get that error message and I'm not sure if this is because my UFO Dead replacement is much more detailed than that of the UFO used for the animation but I guess about at the time in game when it's making the switch the game kinda freezes for a quick moment, not really a biggie I suppose but I'm wondering if there is a way to fix that.
After much hair pulling and teeth grinding I managed to get this script to work:
var explosion : GameObject; var wreck : GameObject;
function ExplodeUFO() { Instantiate(explosion, transform.position, transform.rotation); }
function KillSelf () { yield WaitForSeconds(3); var wreckClone = Instantiate(wreck, transform.position, transform.rotation); Destroy(gameObject); }
I was even able to get my mid air explosion to occure right at the exact keyframe I wanted it. Now to see if I can get a nice big explosion at the end of the animation... after all I figure that speedy UFO is likly to make a big bang lol :D
Answer by Statement · Mar 15, 2011 at 12:04 AM
First, you need to learn to use punctuation. Your first paragraph is quite a read :)
Update can't be a coroutine, meaning you can't yield in it. However there exist an alternative approach on the wiki, CoUpdate, which allow you to yield in an update function similar to Update.
However, you aren't using animation events at all in your posted code.
I still have not been able to figure out how to get an explosion to occure about midway through the UFO's fight path though
Well, you asked a similar question yesterday which I replied to. It should contain code that allow you to use an animation event to explode the UFO after some time into the animation.
Well my animation event at the end call that last part of the code "function killSelf" otherwise my UFO dead replacement would not work, which it does, so I know that part is working.
And the code you posted yesterday I belive was for a situation where the animation was done outside of Unity, when in fact my animation WAS done inside of Unity. So unless I miss my guess that code would not have worked for my situation. plus I'll bet that doing an animation like to one I have here and then trying to get it to match up to my Unity terrain would be pretty tough in another ap like $$anonymous$$ax.
You can add animation events to animations you've created in unity as well as those you import from other programs. I have tried so myself and it works. However I haven't found a way to use the animation editor to set events for animations created with other programs. If you created the animation entirely in unity then you can much simply just create an event directly in the timeline! http://unity3d.com/support/documentation/Components/animeditor-AnimationEvents.html
This is interesting if not a little anoyoing. I tried CoUpdate, It got rid of my error message but my UFO takes off when my player gets close as it should, so far so good right? Ehhh. Wrong, now when I go to where my UFO wreck should be the Intact UFO is there making a buzzing sound, burried in the ground with it's green particle emmiter still going!! What the Frak! Boy just can't win one for the Gipper lol
Hmm, that sounds strange. By the way, what is your intention of having the yield in the update loop? Since you're just waiting 3 seconds and then destroying the object you might just put it in Start as networkZombie says. There's no need to try to destroy it several times. You can try that and back off the CoUpdate approach to see if it helps. If it does, then there's probably some issue with my thoughts about CoUpdate and I'll have to revise the code.
Or you could even do Invoke("$$anonymous$$illSelf", 3); to call $$anonymous$$illSelf after 3 seconds without coroutines. But you still don't want to put it in update but rather in start since putting it in update will create a lot of delayed calls until it is destroyed.
Answer by networkZombie · Mar 15, 2011 at 12:05 AM
yield WaitForseconds() cannot be in the update function. Change to function Start. Have fun =)
Well, I had already tried that long ago and what hapens is, is that the UFO animation does not play at all, ins$$anonymous$$d it just instantly replaces the UFO with the dead replacement UFO right on the spot it's supposed to take off from lol