Hello Guys,
I am following the tutorial series by Brackeys on Youtube, his “Survival Series” I really only got through about the first four as he was discussing basic enemies in their that I thought would be useful to learn for my FPS project.
Anyway, I went along my merry way and had to modify it greatly to make it work, for instance, the tutorial is using a RayCast hit whereas I need to use an OnCollisionEnnter script:
-
created the ‘enemy’ script, and included enemy health, all fine.
-
made an enemy prefab (using the controller mesh), made it rigidbody, for collisions - tagged it “enemyC” for enemy Controller
-
added the ‘enemy’ script to the enemy prefab.
-
modified the bulletPrefab script to detect a collision with a “tag” of “enemyC” - which it did, sent a message to ‘enemy’ script to inflict damage
Now all good so far. Then I added a second enemy and the problem started, which I pretty much expected, because the the bulletPrefab script was using:
if(col.gameObject.tag == "enemyC"){
enemyObject.SendMessage("enemyDamage", 50f);
}
which was no way of differentiating which ‘enemy’ controller is being hit so it continually took the damage down on the object first put into the scene.
So is there anyway to resolve the problem? Or do you know of any tutorials that discuss what to do with multiple enemies?
Thank you for your time and patience :).
Regards,
CK.
I dont know what your enemyObject variable contains but it woild make sense that you inflict the damage to the object that you have collided with.
if(col.gameObject.tag == "enemyC"){
col.gameObject.SendMessage("enemyDamage", 50f);
}
I understand what you are saying, but, both objects have the same tag - and if I “ever” finish this project it will have more than two enemies with the same tag. So it seems Unity will just send of the damage to the first controller it sees.
What I need is some way to create an array for the objects, and have Unity go to the correct object and only take the damage of that object.
I’ve seen code for attacking the closest enemy that creates an array of anything with the same tag:
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy");
And later uses a for…each to go through the array.
But even that won’t help in this instance, because even though it creates an array it won’t actually tell unity which one is being attacked.
And I’m stumped as to how to do it .
So, any and all help is greatly appreciated.
Why do you even care about using FindGameObjectsWithTag to deal damage?
If you hit an enemy with your bullet , you get the collision event object , that you did call “col” in your example.
That object contains all the formation you need. You can check if the tag of the object is equal to “Enemy”.
If yes you now is an enemy.
Then you can just use col.gameObject and you have your enemy game-object.
Now you can use component or SendMessage to execute the damage function (enemyDamage in your case.)
That way you can have a huge number of enemy’s all with the same tag “Enemy” and your script will still work.
1 Like
Your avatar looks familiar where you ever on the GameMaker forums??
But back to the topic at hand: Thankyou, thankyou, thankyou!
From your response I figured out what I was doing wrong! I was setting the GameObject in the Start() function as below, because I thought you had to do that. From the book I was reading the author used Find to link it to the game object in the Heirarchy and then used that to call a SendMessage to call the function in the script.
enemyObject = GameObject.Find("EnemyController");
And that was never going to work, with the GameObject hard coded it was never going to find anything but the first Enemy controller. I suspect you would’ve found that error immediately.
So, reading your reply I thought bugger setting the gameObject, and try to call the function directly, especially if the colliders contains all the information required and tried:
col.gameObject.SendMessage("enemyDamage", 50f);
And it worked!!! Both objects are now receiving damage at different times of course :).
So thanks again - you are a legend :).