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 Punkjim420 · Jun 21, 2012 at 08:25 PM · javascripthealth

Health script and damage script dont work.

these scripts dont work for some reason, i wrote them mostly myself using references, but appearantly i did something i shouldnt have when writing them, my players doesnt get health as far as i know, and my damage script doesnt deal 10 points of damage to my player on collision. how could i fix it so that it does work?

 //player Health script.js//
 
 public var playerCurHp;
 public var playerMaxHp;
 playerCurHp = 100;
 playerMaxHp = 100;
 
 function ApplyDamage (damage : int) {
     playerCurHp -= damage;
     Debug.Log(playerCurHp);
     
     if(playerCurHp > playerMaxHp){
         playerCurHp = playerMaxHp;
     }
 
     if(playerCurHp < 0){
         playerCurHp = 0;
     }
 
     if(playerCurHp < 1){
         //die
         // Die();
         Debug.Log("YOU DIED!");
     }        
     //function Die () {
     //Application.LoadLevel (Application.loadedLevel);
 }


 //**apply damage script.js**//
 var damage = 10;
 
 function OnCollisionEnter (col : Collision) {
     col.gameObject.BroadCastMessage("ApplyDamage", damage,
     SendMessageOptions.DontRequireReceiver);
 }
Comment
Add comment · Show 5
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 Drakestar · Jun 21, 2012 at 08:31 PM 0
Share

"my players doesnt get health as far as i know" is the fundamental problem here. Start the debugger and check code execution and runtime variable states. Does DoDamage actually get called? Do the variables change the way you expect them to change? If everything behaves the way you expect it to at runtime you can ask specific questions.

avatar image Punkjim420 · Jun 21, 2012 at 08:33 PM 0
Share

are you saying to try making collision to force a debug.log of players health? if so, yes i tried that and nothing was ever debugged.

avatar image Drakestar · Jun 21, 2012 at 08:40 PM 0
Share

No, I'm saying you should learn and understand the $$anonymous$$onoDevelop debugger: http://unity3d.com/support/documentation/$$anonymous$$anual/Debugger.html

Alternatively, using Debug.Log statements to print your variable states would be a good start.

avatar image Punkjim420 · Jun 21, 2012 at 08:42 PM 0
Share

oh, i see, thanks, and i tried the alternative. as shown in the code, but it never debug.logged anything.

avatar image Drakestar · Jun 21, 2012 at 08:49 PM 0
Share

That means that your BroadCast$$anonymous$$essage isn't being received. Put a Log in OnCollisionEnter to see if that function is being called. With the debugger, you can set breakpoints to do the same checks easier.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Punkjim420 · Jun 22, 2012 at 03:17 AM

oh, i see, i looked at this, couldnt quite figure out how to do this method of checking. the debugger freezes and lags on my computer when i try to run it, but i did fix my problem by making it a collision check in the health script and if the collided object is tagged as harmful, i lose health by a set amount. ^^ thanks for your help.

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
0

Answer by Berenger · Jun 21, 2012 at 09:14 PM

To what is attached apply damage script.js ? Because it's going to call ApplyDamage on whatever is colliding with what this script is attached to. Meaning, if this is attahe to the player, ApplyDamage will be called on the bullet or whatever does the damage, and the don't have that function. Either send the message on the same gameObject, or use GetComponent to find the health script.

By the way, you're making sure that currHealth is between min and max, there is a function for that : Mathf.Clamp.

Comment
Add comment · Show 2 · 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 Punkjim420 · Jun 22, 2012 at 04:08 AM 0
Share

ok thanks i did that now. inside the same script i call a collider check using tags to find what the object touches and if its the set object it removes the set value of health wanted. :) solved. thanks for your help.

avatar image SirMacJefferson · Jun 22, 2012 at 04:35 AM 0
Share

Glad you solved your problem. You should mark this question as solved now by pressing the green check mark on the answer.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Health Script Help...? 1 Answer

How can I change this script to use GUITexture instead of GUI number, for health display? 1 Answer

Save Player Health and stats 1 Answer

Closure does not work in Unity JS functions? 1 Answer

How Do I Make A Health Bar 6 Answers


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