- Home /
How to get damage variable from another script ?
Hello.
I want to know how to get a damage variable from another object with its own script attached.
I have a collision object that deals damage and 2 sword object with its own script attached.
Swords have a script where they are setting their transform with the players hand and public int Damage.
I have a collision object ( child of a player that gets activated when the player attacks) with script that deals damage to enemy.Collision object has a box collider set to triger and public intingeer called Ddamage; I want to know how can I make the Ddamage have the same number as a damage in the sword ?
I have tried this ( In the start method ( PlayerSword = GetComponnent(); Ddamage = PlayerSword.DAMAGE)
I don't know what is wrong.I must be blind or big Id**t I can't find a way how to make this work.
Does anybody knows how to fix this ?
Answer by cgarossi · Sep 01, 2017 at 09:10 AM
Something like this? (Please reformat your question, it's horrible to look at)
void OnCollisionEnter2D(Collider2D col)
{
var damageScript = col.gameObject.GetComponent<DamageScript>();
var damage = damageScript.damage;
var healthScript = gameObject.GetComponent<HealthScript>();
healthScript.health -= damage;
}
Your answer
Follow this Question
Related Questions
Help! Can't not access the variable linearVelocity of TwistPublisher.cs from another script in Unity 1 Answer
Transform var between scripts 2 Answers
Help w/variables 0 Answers
Why wont variable in other GO get assigned? 1 Answer
How to access Vive controllers in a script attached on a simple game object(a cube)? 0 Answers