- Home /
Static var wont work
The enemy health code I am using
Characterdamage2.js
static var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
Detonate();
}
}
function Detonate () {
// Destroy ourselves
Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
Now this code in my spawning script wont work, it makes the enemies what seems like invincible
CharacterDamage2.hitPoints += 10.0;
my entire spawn code is this
var textColor = Color.red;
var NumberEnemies : int;
static var WaveNumber = 0;
var NewWave : boolean = false;
static var score = 0;
var SpawnPoints : Transform[];
var Enemies : Transform[];
function Awake () {
guiText.material.color = textColor;
}
function Update(){
NumberEnemies = ((GameObject.FindGameObjectsWithTag("Enemy").Length) - 1);
if(NumberEnemies == 0 && !NewWave){
NewWave = true;
WaveNumber += 1;
SpawnEnemies();
}
}
function SpawnEnemies(){
yield WaitForSeconds (3);
CharacterDamage2.hitPoints += 1.0;
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[0].position,SpawnPoints[0].rotation);
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[1].position,SpawnPoints[1].rotation);
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[2].position,SpawnPoints[2].rotation);
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[3].position,SpawnPoints[3].rotation);
SpawnEnemy = Instantiate(Enemies[0],SpawnPoints[4].position,SpawnPoints[4].rotation);
NewWave = false;
}
function OnGUI () {
GUI.Box(Rect(10,15,75,35), "Score: " +score);
GUI.Box(Rect(10,105,75,35), "Wave: " +WaveNumber);
}
Answer by Eric5h5 · Sep 16, 2013 at 12:35 AM
You can't use static vars like that; static means there's only one instance of that variable regardless of how many objects are using the script. Remove static from all variables unless you actually mean for there to be one instance and fully understand all the consequences of using static.
Oh okay, is there anyway I can have it so after every wave the enemy gains 10 health?
What am I doing wrong? var CharacterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2); CharacterDamage2.hitPoints += 1.0();
Answer by oliver-jones · Sep 16, 2013 at 09:58 PM
You need to access your health variable WITHOUT using a static variable. Just remove the 'static' bit, and you should be good to go. If you need to actually access your health variable externally, then you need to use GetComponent();
I did this and got "An instance of type 'CharacterDamage2' is requited to access non static member 'hitpoints'.
Thats because you're trying to access it 'statically' here:
function SpawnEnemies(){
yield WaitForSeconds (3);
CharacterDamage2.hitPoints += 1.0;
You can't access it as a static (well, you shouldn't as thats causing your problem), as Eric mentioned, you want to use the link he has provided.
What am I doing wrong? var CharacterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2); CharacterDamage2.hitPoints += 1.0();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Accessing other script problem 1 Answer
Temperary Variables 0 Answers
How do I deselect an object when another is selected? 2 Answers
Unity mathematics phrasing. 1 Answer