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 SepM · Jul 04, 2014 at 03:17 PM · staticvarfightingzerohealth

Health is set to zero at start of game.

I'm making a fighting game and, of course, it has health. I started the game with a static variable for the health.

  static var Health1 = 1000;

But it didn't decrease when it collided with a projectile. So I decided to switch it to a regular variable and the health started out as 0! I figured that the problem didn't lie within the collision but within the health variable itself. Here's all of the references of "Health1" in my script.

     var Health1 : int = 1000;
     var Energy1 : int = 500;
     function Update () {
     //Limits your health.
         if(Health1 > 1000){
             Health1 = 1000;
         }
         if(Health1 < 0){
             Health1 = 0;
         }
 }
 

    function OnCollision(collision : Collision){
     if (collision.gameObject.tag == "R_Projectile"){
             //Receive variables from the attacker to properly calculate the outcome.
             Damage = GetComponent(r_projectile2).Damage2;
                     Health1 = Health1 - Damage/DEF1;
                     Energy1 = ((Energy1 + Damage/DEF1)/2)*RATE1;
                     DMG1 = true;
     }
     }

I removed a good amount of script that doesn't affect the problem. Hope something can be found out here.

Comment
Add comment
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

3 Replies

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

Answer by podmaster · Jul 04, 2014 at 04:02 PM

When you define your health variable do it like this

     var Health1 : int ;



and set the value from the inspector, selecting the gameobject. and i recommend you to try also putting your if code this:

   if(Health1 > 1000){
             Health1 = 1000;
         }
         if(Health1 < 0){
             Health1 = 0;
         }


inside a function and call it after applying damage. and not from Update() maybe this is causing the problem too.

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 earrgames · Jul 04, 2014 at 04:03 PM

Ok, the problem is that that variable is set up from the inspector, it should be at "0", but you can better do this, Add this to your code:

function Awake(){ Health1 = 1000; }

Cheers!

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 Lazdude17 · Jul 04, 2014 at 04:01 PM

I don't think OnCollision is a method. You have to use OnCollisionEnter, OnCollisionStay or OnCollisionExit.

Also your projectile should be a trigger collider if it's not. If it is you need to change

 OnCollisionEnter(collision : Collision)

to

 OnTriggerEnter(collision : Collider)


Comment
Add comment · Show 1 · 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 podmaster · Jul 04, 2014 at 04:03 PM 0
Share

and i recommend to create the collision function on the proyectile is easier to send the damage parameter then your player just have to run an "takeDamage" function.

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

24 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

guitexture a static var? 2 Answers

Increase Static Var gradually 1 Answer

How to use a static var 2 Answers

Static variables heavy for the iphone's CPU? 3 Answers

Static variables explanation. 2 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