- Home /
Not asigned health script
Hi, I am having a small problem which I don't understand.
I am importing health script corectly in my EnemyAI but when it's about taking damage I get the error that the component is not asigned.
public HealthBarGUI health;
void Awake(){
myTransform = transform;
controller = GetComponent<CharacterController>();
health = GetComponent<HealthBarGUI>();
currentWaypoint = WayPoint1;
.
.
.
.
.
void Attack (){
if( attackDelay == 0 )
attackDelay ++;
animation.CrossFade(attackAnimation.name);
health.curHealth = health.curHealth - maxDamage;
if( attackDelay > 3)
attackDelay = 0;
}
}
And the error I get is the following :
"Object reference not set to an instance of an object on line health.curHealth" EnemyAIC.Attack
on your start and attack, can you do a simple Debug.Log(health == null); see if it finds it initially, and that it doesn't somehow get removed before attack happens? If the check fails on the awake function, you dont have the script on the gameobject. and if the attack check fails, it's getting removed from somewhere.
Another simple thing to check is there is actually a HealthBarGUI on the gameobject in the first place. Calling GetComponent correctly will still return null if there isn't one there to be found.
the debug log showed me : "Error you don't want" in start for health == null Altrough I don't know why because I have the script attached to my main character :-?
Didn't you say you were attaching this script to enemies too? GetComponent is not searching your whole scene for the GUI health component, just the current object.
In the inspector appears the object containting that script like
Health $$anonymous$$AX(HealthBarGUI)
Answer by AMDAndrew · Oct 25, 2012 at 09:27 PM
I just found out that without this declaration
health = GetComponent<HealthBarGUI>();
it will work just fine but you have to attach the script by yourself.
Your answer
Follow this Question
Related Questions
Static variable issue in published 1 Answer
My static variable is not changing 2 Answers
Global Variables Refuse to Cooperate 1 Answer
Changing static variables from another script? 1 Answer
Global Varible Problem 2 Answers