- Home /
killing a script from another
I am trying to kill a script from another script. i have seen dozens of examples and tried to alter them to fit my needs with no luck. half the time I get errors from the scripts even without altering them. Here is my situation. i have an enemy with animations and he is following a behaviour script ( called "EnemyBehaviour"). I found it and it works fine.The enemy sees me, runs at me and attacks. and attacks and attacks. I want him to stop by punching him. I also have scripts for that where I can run around and punch other objects to make them animate. What I want is to punch the enemy and have him die - or play an animation called "MiaDie". Now THAT much I can even do - if I turn off the EnemyBehaviour script. With the EnemyBaviour script enabled, I can see he starts to die for a few frames but then the attacking still continues. So I want to add to the script below something to the effect of GetComponent.script.enable = false. Or whatever to stop this guy from hitting me`var KillMia : AnimationClip;
function OnTriggerEnter (fist : Collider) {
if(fist.tag=="Fist")
GameObject.Find("newmia").animation.Play("MiaDie");
}
this is a javascript attached to the miamodel. Is the fact that I am trying to kill a C# script ( EnemyBehaviour) with a javascript an issue? do they not play well together. I Forest Gump my way through programming by using what I find so if anyone has a C# code for thisI'm fine with that
You can add a static boolean to your script, and execute the ennemy's behavior only if your boolean is true. Then, when you kill the enemy, you just have to set this static bool to false.
Answer by Baste · May 21, 2015 at 09:35 AM
The easiest thing is to set the script to inactive - that will stop all of the calls to Update. BUT UnityScript ("JavaScript") and C# scripts can't talk together out of the box - so you can't do a GetComponent to find the EnemyBehaviour script.
A UnityScript script cannot "see" C# script unless they're compiled before the UnityScript script. There's a pretty easy way to fix that - you move all of the C# scripts to a folder that compiles earlier (like the Plugins folder), and you're good to go. There's a more thorough explanation here.
That being said, mixing languages is hard. I also find that UnityScript, while having some nice features, actually becomes harder to use once you get into it. There's also not very many resources online about it since it's only used in the context of Unity, and it's also wildly different from the JavaScript that's used everywhere else - so if you google for solutions to "JavaScript" issues, you'll never find an answer that's actually applicable to Unity.
Because of that, I'd suggest you drop UnityScript and stick to C#. In that case, your problem is simple to solve:
var enemy = GameObject.Find("newmia");
enemy.animation.Play("MiaDie");
enemy.GetComponent<EnemyScript>().enabled = false;
Sorry posted as answer not comment. i see how this kills the running script and plays the death animation but where is the whole trigger instruction of the "collider tagged "fist" making this all happen - or the C# variation of what I had with the OnTriggerEnter ?
I'll try That. Thanks for all the info. But just looking at it I am a bit confused. I see it kills the script and plays the death but where is the trigger to set it all in action? The collider tagged "fist". Or the C# version of that?
I just skipped that part, as it's the same thing in both languages. Functions are declared differently, but you'll figure that out on your own.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200121im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
help with animation 1 Answer
[Unity5] Performance and functions 3 Answers
How to make script affect only one this game object 2 Answers
Characterdoesn't run on Aiming mode 0 Answers