- Home /
From script A in script B
I have "script A" (enemy)
var damage : float = 5;
And have "script B" (player)
var Health : float = 100;
what i can make for make this:
//exemple
if(press button "x")
Health = Health - damage
Answer by _1 · Mar 03, 2014 at 04:56 PM
put this in:
var health : float = 100;
function Update()
{
var enemyComponent = GameObject.Find("enemy").GetComponent(enemyScript);
if(Input.GetKeyDown(KeyCode.X))
{
health = health - enemyComponent.damage;
}
}
NullReferenceException: Object reference not set to an instance of an object health.Update () (at Assets/script/health.js:42)
i make like you but....
That means either: -There's no gameObject named "Enemy" in the children or -There's not an enemyScript object component on that gameobject
thx. i make a gameobject enemy and work. yeeeaaaaaaaaaaa
Don't do GameObject.Find in Update()!
And because there are many enemies and one player, you should have the instance enemyScript apply the damage to the playerScript.
var playerScript : PlayerScript;
function Awake () {
playerScript = GameObject.FindWithTag("Player").GetComponent.<PlayerScript>();
}
function HitPlayer() {
playerScript.health -= damage;
}
Your answer
Follow this Question
Related Questions
Health Regeneration 2 Answers
Damage/Health problem 2 Answers
Weapon Mesh Damage 1 Answer
How to get Enemy to break wall between it and Player. 1 Answer
Player Health Damage 2 Answers