Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by davethewave7 · Apr 24, 2015 at 06:04 PM · javascriptcolliderontriggerenterhealthbarhealth-deduction

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davethewave7 · May 11, 2015 at 03:02 PM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges