- Home /
Turn on Script when collide with this object?
I have a script attached to numerous NPCs and I want to send a message to turn on the script for ONLY the NPC that I have collided with and not anyone else. I don't want the 'NpcBattleScript' to turn on for every NPC that has 'npc' tag or has the script attached. I also want to turn off that script from within the script after it has finished doing a particular thing. Can anyone point me in the right direction?
I know:
if(hit.collider.gameObject.tag == "npc")
{
// in here I want to say, turn on NpcBattleScript
}
in the other script I want something like:
if (battleOver = true)
{
//turn off this script
}
Answer by getyour411 · Feb 22, 2014 at 11:59 PM
Example:
<NpcBattleScript> npcBattleScript = hit.collider.gameObject.GetComponent<NpcBattleScript>();
npcBattleScript.Somefunction();
npcBattleScript.someVar = 24;
etc.
For turning off script, one of many UA posts on that
http://answers.unity3d.com/questions/39748/disable-script-from-code.html
Thanks for your answer. I used the link you provided and had a couple of errors to do with how I'd set up my code, but managed to get it working. I'd attempted it previously, but couldn't get it to work, but I got there in the end. Thanks for your help and getting back to me so quickly.
Good deal. If you happen to see this, next to the Thumbs Up/Down icons there's an option to accept the solution, please tick it which will mark this question as closed - thank you.
Your answer
Follow this Question
Related Questions
Unable to enable script 0 Answers
enable/disable specific components 3 Answers
disable/enable script js 2 Answers
How To Disable A Prefab? 2 Answers