Make a bullet know who fired it

Hi. So I’ve a 2d top down space shooter with player, friendly, and enemy fighters zipping around. I have two types of bullets (prefabs). They’re identical copies of each other except for the coloring. One type is used by friendlies, one by enemies.

The bullets are all pooled and recycled, so are just setActive when a ship fires them.

How do I get the bullet to know WHO fired it, because I need to set kills of the player and each of his allies, yet they all use the same bullets. Same situation with the enemies.

I was thinking something like OnEnable, I do some sort of circular raycast (or a straight one aimed behind) to find the nearest game object, because that MUST be the one who fired it, in theory. I just don’t know enough about this area to succeed.

Can anyone provide a sample code, or even a simpler, more efficient solution to the problem?

Thanks a mil
-Kevin

The ‘right’ answer is to have the object that fired the bullet to tell the bullet. You can do this by changing the bullets name or its tag. If you have a script on the bullet, you can store the information in a component and use either SendMessage() or directly accessing the component to set the value.

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://unitygems.com/script-interaction1/

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

And if you really want to do your ‘raycasting thing’, you could do it by calling Physics.OverlapSphere() and then sorting through the results to find an object of the time that could have fired the bullet (*but I don’t recommend this solution).