- Home /
Problem with getcomponent replacing static variables
Hi everybody
I've been working on replacing the static variables in the script I use for my enemies, because I want to add more then 1 enemy. So I've replaced the static variable used for the enemies health by a normal variable I reach using FindWithTag(All my enemies have the tag Enemy) and GetComponent. But there's a problem in my script. The enemy immediately dies when the player is instantiated
These are my scripts:
The script attached to the enemy prefab, EnemyProximity:
static var aggroDistance = 10;
var mytransform : Transform;
var otherobject : GameObject;
var othertransform : Transform;
var glitchCorrector : boolean = true;
var tickerInterval : boolean = false;
var tickerRate = 5.0;
private var nextTick = 0.0;
var drop : GameObject;
var enemyHealth : int;
static var seppuku : boolean = false;
var attackscript : AttackScript;
function Awake (){
otherobject = null;
othertransform = null;
enemyHealth = 100;
}
function Update () {
if (ClassSelectionScript.classselected == true && otherobject == null && othertransform == null ){
otherobject = GameObject.FindWithTag("Player");
othertransform = otherobject.GetComponent(Transform);
attackscript = otherobject.GetComponent(AttackScript);
}
if (attackscript != null && otherobject != null && attackscript.enemyHealth <= enemyHealth){
enemyHealth = attackscript.enemyHealth;
}
if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && glitchCorrector == true){
ArcherHealthScript.curhealth += 10;
WarriorHealthScript.curhealth += 10;
MageHealthScript.curhealth += 10;
glitchCorrector = false;
}
if (othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) > aggroDistance && glitchCorrector == false){
glitchCorrector = true;
}
if(othertransform != null && Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
ArcherHealthScript.curhealth -= 10;
MageHealthScript.curhealth -= 10;
WarriorHealthScript.curhealth -= 10;
tickerInterval = true;
}
if(tickerInterval == true && Time.time > nextTick){
nextTick = Time.time + tickerRate;
tickerInterval = false;
}
if(enemyHealth <= 0){
seppuku = true;
Destroy(gameObject);
var instance : GameObject = Instantiate(drop, transform.position+Vector3(0,0.2,0), transform.rotation);
ExperienceScript.curexp += 10;
}
}
And the script attached to the player, called AttackScript:
static var distance = 10;
var myobject : GameObject;
var otherobject : GameObject;
var mytransform : Transform;
var othertransform : Transform;
var cooldown : boolean = false;
var activetexture : GUIStyle;
var inactivetexture : GUIStyle;
var usedtexture : GUIStyle;
var enemyproximity : EnemyProximity;
var enemyHealth : int;
static var interval : float;
function Start (){
usedtexture = activetexture;
myobject = GameObject.FindWithTag("Player");
otherobject = GameObject.FindWithTag("Enemy");
mytransform = myobject.GetComponent(Transform);
othertransform = otherobject.GetComponent(Transform);
enemyproximity = otherobject.GetComponent(EnemyProximity);
enemyHealth = enemyproximity.enemyHealth;
interval = 10;
}
function OnGUI (){
if (GUI.Button (Rect(0,500,100,100),GUIContent("Attack"),usedtexture) && Vector3.Distance(mytransform.position, othertransform.position) < distance && enemyHealth > 0 && cooldown == false){
enemyHealth -= 50;
cooldown = true;
Timer();
}
}
function Update (){
if (enemyproximity.enemyHealth > enemyHealth){
enemyHealth = enemyproximity.enemyHealth;
}
}
function Timer (){
if(cooldown == true){
usedtexture = inactivetexture;
yield WaitForSeconds(interval);
cooldown = false;
usedtexture = activetexture;
}
}
The problem is caused by .GetComponent I think. And I also get a error in line 27 of the EnemyProximity script, saying object reference not set to an instance of an object.
Please help me solve these problems!
Unity says the problem is in the if statement, maybe I've made a mistake with < and <=?
What does the error Object reference not set to an instance of an object mean?
It means the thing you are trying to reference doesn't exist.
But all the things I reference exist, there is a variable in attackscript called enemyHealth, and the variables otherobject and attackscript do exist. How is it possible Unity thinks those things do not exist?
$$anonymous$$ore specifically, Unity is telling you that the variable that should reference the object is null. If line 27 is what I suppose, you should revert the expressions order to avoid this problem:
if (attackscript != null && otherobject != null && attackscript.enemyHealth The code generated here verifies attackscript first; if it's not null, otherobject is verified; finally, if both are non null, attackscript.enemyHealth is compared. This is a smart feature created in C and used in all modern languages: conditional expressions are evaluated from left to right, and only if necessary - in this case, the first false result aborts the whole if, thus the other expressions are not evaluated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064324im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player Damage to enemy damage melee system 0 Answers
Dungeon Enemy Help 1 Answer
Player attacks once but the enemy takes double damages! 1 Answer
Question on Player Damage 1 Answer
auto select enemys 3 Answers