Get Variable through collision

ihave this script that gets a variable from as script but how could i make it to get the variable from the object it collided wiht

function OnCollisionEnter( collision : Collision ){
if (collision.gameObject.tag == "enemy" ){
BasicBehavior.enemyhealth -= stabdammage;
 }
}

i tried this but it didint work collision.gameOjbect.BasicBehavior.enemyhealth -= stabdammage;

You’re probably looking for collision.gameObject.stabdamage;

But I don’t think that’ll work either. You need to find the script where stabdamage is defined. Let’s call that script name [Class].

Then I think you might get it working with something like this:

function OnCollisionEnter( collision : Collision ){
if (collision.gameObject.tag == "enemy" ){
    [class] myScript = ( [class] ) collision.gameObject;
    BasicBehavior.enemyhealth -= myScript.stabdammage;
 }
}

Of course, this might not be your problem and it’s even less likely to be your solution. And If your stabdamage is defined in several different scripts, then I don’t know how to help you.

Good Luck!

Just use GetComponent.

–Eric

So there might be three enemies but if i damage one it will dmaga all how could i make the enemy i hits script variable health.

function OnCollisionEnter( collision : Collision ){
if (collision.gameObject.tag == "enemy" ){
    [class] myScript = collision.gameObject.GetComponent<[class]>();
    BasicBehavior.enemyhealth -= myScript.stabdammage;
 }
}

Eric is right, of course. Sorry, late night posting.
Try that. :slight_smile:

does not work.

sorry.