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 yvyv1000 · Oct 11, 2013 at 06:27 PM · damage

player damage script help

hello i have made a script for the health of my player. but i can't make a script for te damage can you help me please

this my script

 var curHealth : int = 100;
 var maxHealth : int = 100;
 var healthtext : GUIText;
 
 function Start () 
 {
     healthRegen();
 }
 
 function Update () 
 {
     healthtext.text = curHealth + " / " + maxHealth;
 
     if(curHealth < 0 ) 
     {
         curHealth = 0;
     }
 
     if(curHealth > 100) 
     {
         curHealth = 100;
     }
 
     if(Input.GetKeyDown("e")) 
     {
         curHealth -= 10;
     }
 }
 
 function healthRegen () 
 {
     for(i=1;i>0;i++) 
     {
         yield WaitForSeconds(0.5);
 
         if(curHealth < maxHealth) 
         {
             curHealth++;
         }
     }
 }
Comment
Add comment · Show 7
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 Conect11 · Oct 11, 2013 at 06:49 PM 0
Share

First off, welcome to Unity. Second, this isn't meant to be snarky, but that's not your script, it's JesseEtzler's from Youtube. Credit where credit is due. That said, making a damage script really isn't difficult. Pay attention to whatever you titled this one. Caps count! Then, in the script you want to inflict the damage from you want to call your curhealth, which simply means "current health." Here's how that line would look:

 Scripttitle.curhealth

that's calling the script, and the component inside of the script called "curhealth". So it would look something like this:

 Playerhealth.curHealth -= damage;

Ok, so what the heck is that thing there called "damage"? glad you asked. That's referring to a variable, or something that you can change in the inspector. (so that you can use this same script on multiple enemies with different damage based on the enemy.) You could just make this a number, ins$$anonymous$$d of using the damage variable, but to each their own.

So what you're looking at is you need a separate damage script, attached to a gameobject, be it an enemy or an enemy's weapon. How you receive the damage is up to you, it can be via onTriggerEnter, onCollissionEnter, whatever. $$anonymous$$ake sure that if you use triggers that "Trigger?" is checked on any collider. Hope that gives you some help. God bless.

avatar image clunk47 Conect11 · Oct 11, 2013 at 06:51 PM 2
Share
 PlayerHealth.curHealth


would be calling the variable curHealth as if it were static. To call something non static, but simply public, you need to use GetComponent.

avatar image Conect11 Conect11 · Oct 11, 2013 at 06:53 PM 0
Share

ah, see, I moved all my vars to static so they could be called from elsewhere. (clearly could do that other ways, just that's the first way I learned) :)

avatar image clunk47 Conect11 · Oct 11, 2013 at 06:59 PM 2
Share

Understood, just remember that easy access isn't the only reason static exists, static variable can only have ONE instance, and most of the time needs to be reset manually, so it can get messy if misused. I usually only use static if I'm doing Editor scripts, or for classes that derive from System.Object ins$$anonymous$$d of $$anonymous$$onoBehaviour. I definitely would recommend using GetComponent when accessing variables from other scripts or components. I actually started using static for the same reasons you probably did, then found out why I shouldn't, at least in Unity.

Show more comments
avatar image clunk47 · Oct 11, 2013 at 07:47 PM 1
Share

It's all good man, you're doing quite well so far. You put more effort into asking your questions than many, and you're always specific and helpful. I just like to point others in the right direction. Now that you know to look more into GetComponent ins$$anonymous$$d of using static, that's a step forward ;)

avatar image vexe · Oct 28, 2013 at 09:12 PM 0
Share

I moved all my vars to static so they could be called from elsewhere

That literally butchered me! @_@

@Conect11 please read my answer here - skip to the good coding habits to know when and when not to use a static variable.

1 Reply

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

Answer by clunk47 · Oct 11, 2013 at 06:32 PM

I'm not sure you asked your question in the correct manner... This script you have written works. I press 'e', health is subtracted, then regeneration causes health to rebuild...

EDIT: I've taken out the for() loop (Thx @fafase):

 #pragma strict  
 
 var curHealth : int = 100;
 var maxHealth : int = 100;
 var healthtext : GUIText;
 
 function Start ()
 {
     healthRegen();
 }
 
 function Update ()
 {
     healthtext.text = curHealth + " / " + maxHealth;
     
     if(curHealth < 0 )
     {
         curHealth = 0;
     }
     
     if(curHealth > 100)
     {
         curHealth = 100;
     }
     
     if(Input.GetKeyDown("e"))
     {
         curHealth -= 10;
     }
 }
 
 function healthRegen ()
 {
     while(true)
     {
         yield WaitForSeconds(0.5);
         if(curHealth < maxHealth)
             curHealth++;
         yield WaitForEndOfFrame();
     }
 }

Comment
Add comment · Show 4 · 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 fafase · Oct 11, 2013 at 06:39 PM 1
Share

The healthRegen is kinda weird. An infinite for loop, I'd rather go with a InvokeRepeating, just a matter of taste though.

avatar image fafase · Oct 11, 2013 at 06:45 PM 1
Share

I guess the point is just to start a loop that will never end. It starts from 1 and check with 0 which will never happen and hence never stop the loop and constantly refill the health. Still not a good way.

avatar image clunk47 · Oct 11, 2013 at 06:46 PM 1
Share

Nice observation. I didn't take the time to rewrite, just to make the for() statement work, but it doesn't even need to be there...

avatar image saadiq0529 · Aug 26, 2014 at 09:56 PM 0
Share

Hey i keep getting a problem with this script it keep saying expecting } found "

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ai Aint Taking Damage From Bullet (FPS Rocket) 1 Answer

Structuring a damage and kill count method... 1 Answer

Damage on collision with player.. 1 Answer

How Can One Collider Recognize Contact With Another? 0 Answers

My enemy wont take damage 0 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