- Home /
How to have a cloned/duplicated enemy react the same way the original does?
Ok so I have a typical monster enemy that chases the player, and even got a script to work that makes the enemy chase the player from different distances based on if the player's flashlight is in. It's really cool.
The problem is, when I either duplicate the enemy (in the Hierarchy) or create clones with a spawner script, the replicas do not react to the flashlight going on and off.
Basically, the flashlight has a gameobject variable that you drag onto the script component to know which object is the enemy. It works, but only for the original enemy, and the replicas are stuck on the default chase distance whether the flashlight is on or off.
I know it has something to do with setting the game object right? Or, is it even possible to fix this? Because when you turn on the flashlight it changes a variable in the AI script of the enemy (which I have set in my flashlight component box) and this variable is the distance at which the enemy will chase the player. So, when there are multiple enemies, the flashlight will still only change the distance of the original and the other enemies' chase variable is stuck on the default.
Is there any way I can get multiple copies of an enemy react when this light goes on and off?
Answer by Bunny83 · Aug 19, 2012 at 03:12 AM
Well, if the flashlight script should "serve" multiple enemies, your script would need to store multiple references to access all enemies. This approach is not really straight forward. An advanced programmer might have used a delegate to propagate the information, but the easier solution is usually to have the enemy check the flashlight state itself.
The enemy would obtain a reference of your flashlight in Start() and use this reference to read the flashlights state whenever it needs to.
I think it doesn't make much sense to post any code since we don't know your language and don't know how your script looks like or works.
Yeah, that makes more sense. $$anonymous$$y enemy script is in C# and my flashlight script is in UnityScript, so it's sort of complicated. Basically I was using Get component to edit the C# variable from the flashlight.js script, so I guess I just need to do something in C# that can check if the flashlight's light component is enabled and adjust the variable accordingly. Is there a specific way to check if components of another Game Object are enabled through c# that is different from unityscript?
Just do:
//this should go in Start()
FlashLightClass FL = OtherObject.GetComponent<FlashLightClass>();
//use this when doing flashlight specific code
if(FL != null)
{
//code that relies on having a flashlight
}
I was just co$$anonymous$$g back to say I got it to work by what Bunny83 had said. I simply put an if statement that checked if the light itself was enabled or not (which is simpler than checking variables inside a script i think) and since this is inside the AI script, all the duplicates will react accordingly. Thanks for the insight, really helped to look at things from a different perspective.
If there's only one flashlight script, you can use FindObjectOfType to get the flashlight script directly. Of course use it only once in start and not somewhere in Update ;)
$$anonymous$$ixing languages does work, but should be avoided since it will get you in trouble any time soon ;) If you have a lot quite long JS scripts you can easily "convert" them to C# by creating a standalong build and use ILSpy to open the UnityScript assembly and decompile it to C#. Note that coroutines can't be decompiled to C# since the compiler "cheats" behind the scenes. They can be viewed in IL, but that doesn't help much ;)
Yes, my next project is going to be all in the same language. Ha, and I should try that ILSpy thing, that sounds cool! Thanks
Your answer
Follow this Question
Related Questions
Question about instantiate AI 0 Answers
Can't make enemy respawn 0 Answers
clone object make a new script 0 Answers
Trying to Instantiate() an object without cloning the code 2 Answers
Why won't my AI spawning script work? 2 Answers