How to get at a script variable upon collision?

Hello,

Let me paint the scenario that I need some help with:

In my game I have three GameObjects which are prefabs. Each prefab has the usual stuff.

Prefab_badGuy has a mesh, rigidbody, etc and has a script called badGuy_AI.js

Prefab_badGuyBullet has all the same and has a script called badGuyBullet_AI.js

Prefab_player has all the same and has a script called Player.js.

In the Player.js script I have a function OnCollisionEnter(collision : Collision).

When I collide with either the instantiation of the Prefab_badGuy or Prefab_badGuyBullet a print (gameObject.name) prints out the gameObject name properly on the status line so collision is working properly.

Inside both the badGuy_AI.js and badGuyBullet_AI.js I have a variable declared ‘var Alliance : int;’.

I want to access this Alliance variable in the Player.js OnCollisionEnter(collision : Collision) when either the instantiation of the Prefab_badGuy or Prefab_badGuyBullet object collides with the player.

The syntax as I understand it is:

if (collision.gameObject.GetComponent(ScriptName).Alliance == 1) return;

But ‘ScriptName’ must be replaced with badGuy_AI.js or badGuyBullet_AI.js.

But I don’t know which object has collided with the player so I don’t know which .js to put into ScriptName.

How do I get around this???

You want two things to behave the same way when they are given certain information - this is a hint that you may want to use inheritance or separate your classes farther.

I’d recommend creating a Damagable class, and attach that to everything that can take damage. This way you say

if ( collision.gameObject.GetComponent( Damagable ) )

to determine if an object can take damage. Damagable could be set up to automagically be given an alliance by badGuys and badGuyBullets and whateversuch -

badGuy.Start() {
  GetComponent(Damagable).alliance = Alliances.BadGuys
}