- Home /
Duplicate Question
How to call function from another script ?
Hello
i have a player script with damage simulation :
private var ennemy : Ennemy ;
function Update ()
{
if(Input.GetKeyUp(KeyCode.R))
{
ennemy = GameObject.Find("Ennemy").GetComponent(Ennemy) ;
ennemy.ApplyDamage = 10 ;
}
i have a ennemy script with life,armor,and function apply damage :
var lifeEnnemy : int ;
var armorEnnemy : int ;
function ApplyDamage (damage : int){
if(armorEnnemy <= 0)
{
lifeEnnemy -= damage ;
}
else if(armorEnnemy < damage )
{
var diff : int ;
diff = damage - armorEnnemy ;
armorEnnemy -= armorEnnemy ;
lifeEnnemy -= diff ;
}
else
{
armorEnnemy -= damage ;
}
}
But i want creat a new script : "global" with lifeEnnemy and armorEnnemy and function applydamage.And modify my ennemy script in :
private var ennemy : GlobalVar ;
function Start ()
{
ennemy = GameObject.Find("Player").GetComponent(GlobalVar) ;
ennemy.lifeEnnemy = 50 ;
ennemy = GameObject.Find("Player").GetComponent(GlobalVar) ;
ennemy.armorEnnemy = 50 ;
}
+ my function apply damage
So i could make diferent ennemy with different life and armor.
i know how to call a variable with get component but i don't know how call a function ?
Hope it s clear ?
Thank s !
this question have been asked 1000s of times, for god sake please google first before posting http://bit.ly/1kDYN$$anonymous$$C
Answer by Mysta · Feb 12, 2014 at 09:42 PM
I could be mistaken but i believe at the beginning you would do something like the following:
//if the script with applydmage is called ScriptWithDamage.cs
private ScriptWithDamage damageScript;
//in awake
void Awake()
{
damagescript = GetComponent<ScriptWithDamage>();
}
//Then when you want to call it
damagescript.ApplyDamage(10);
i don t understand, i must use awake function to call my applydamage function ?
(sorry i work with javascript)