- Home /
What is wrong with my HealthBar/Damage Script?
Hi, I have written a HealthBar script for the player, and I am trying to connect it with a damage script.
Here is my HealthBar Script:
#pragma strict
var damageScript : DamageScript;
damageScript = GetComponent(DamageScript);
var damage;
damage = damageScript.damage;
var barDisplay : float = (percent/100)*clockFGMaxWidth;
var percent : float;
var clockFGMaxWidth : float;
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
var health : float = 100;
function OnGUI(){
{
var gap : float;
GUI.BeginGroup (new Rect(Screen.width-progressBarEmpty.width-gap,gap,progressBarEmpty.width,progressBarEmpty.height));
GUI.DrawTexture(Rect(0,0,progressBarEmpty.width,progressBarEmpty.height),progressBarEmpty);
GUI.BeginGroup (new Rect(5,6,barDisplay,progressBarFull.height));
GUI.DrawTexture(Rect(0,0,progressBarFull.width,progressBarFull.height),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
var isPastHalfway:boolean=percent<50;
var rot:float=(percent/100)*360;
var startMatrix:Matrix4x4=GUI.matrix;
}
}
function Update () {
barDisplay = health*(5.0);
if(barDisplay<=0) {
Destroy(gameObject);
}
}
function Start() {
}
function OnTriggerEnter (collision : Collider) {
if (collision.gameObject.name == damageScript.Self) {
damageScript.Damage();
}
}
public function ApplyDamage() {
var RealDamage : int = damage;
health -= RealDamage;
var Newhealth : float = health - RealDamage;
barDisplay = Newhealth;
}
Here is my Damage Script:
#pragma strict
public var Self : String;
static var damage : int;
static var PlayerHealth : HealthBarJava;
PlayerHealth = GetComponent(HealthBarJava);
function Start() {
var CriticalStrike = Random.Range(1, 11);
Debug.Log(CriticalStrike);
if ((2.0) < CriticalStrike) {
damage = 0;
} else if (CriticalStrike < (10.0)) {
damage = 10;
} else if ((9.0) < CriticalStrike) {
damage = 20;
}
}
static function Damage () {
var CriticalStrike = Random.Range(1, 11);
Debug.Log(CriticalStrike);
if ((2.0) < CriticalStrike) {
damage = 0;
} else if (CriticalStrike < (10.0)) {
damage = 10;
} else if ((9.0) < CriticalStrike) {
damage = 20;
}
AD();
}
static function AD () {
PlayerHealth.ApplyDamage();
}
I keep getting this Error:
NullReferenceException: Object reference not set to an instance of an object
HealthBarJava.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/Scripts/Health Bar/Scripts/HealthBarJava.js:43)
I dont understand the problem, please help if you know how.
I'm trying to make it so when the player runs into an object (Self), it deals damage to the player. But, every time I collide into the object (Self), I get the error.
NullReferenceException: Object reference not set to an instance of an object HealthBarJavaOriginal.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/Scripts/Health Bar/Scripts/HealthBarJavaOriginal.js:43)
Answer by Graham-Dunnett · Apr 24, 2015 at 06:09 PM
The error message says it's line 43 of your HealthBarJava.js. And that it's inside the OnTriggerEnter() function. Your OnTriggerEnter() function only has two real lines of code. Line 43 is using your damageScript
variable. Nowhere in your code can I see you check if that variable is assigned. Your line 4 looks it up, but how do you know it's been found? Probably worth checking that.
Answer by Landern · Apr 24, 2015 at 06:12 PM
A static member is said to exist on the class, you will call a static function/method by the Class name. In the case of javascript/unityscript you don't have to define the class, the file that the script exists in is typically the name(case sensitive) of the file.js. In your case in the OnTriggerEnter method change:
damageScript.Damage();
to
DamageScript.Damage();
so long story short, Damage as a method/function exists on the class, Self for instance actually exists on the object instance and is fine to call it the way you did already since the lower case d in damageScript is actually an instance of that script.
Your answer
Follow this Question
Related Questions
How to use OnTriggerEnter? (JS) 1 Answer
OnTriggerEnter (JS) 1 Answer
percentage health bar 1 Answer
Multiple OnTriggerEnter on One Collider 1 Answer
Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer