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 Doneyes · Sep 06, 2013 at 01:41 AM · debuglogprinthealth

Health value isn't subtracting correctly

So in my script, it seems as if the health value doesn't get changed at all. On line 8 it prints the health value (which I set to be 100) and on line 26 it correctly prints the health with damage subtracted. The issue is that the actual health value (the one in another game object) doesn't get changed at all. I can manually change it and it's all good, but the script below wont actually change it. It just prints on line 26 the value as if it were changed.

 function OnCollisionEnter(other : Collision) {
     if(other.collider.GetComponent(HealthSystem)) {
         Debug.Log("This1");
         HealthS = other.collider.GetComponent(HealthSystem);
         Debug.Log("This2");
         Health = HealthS.Central.GetComponent(CentralHealth).Health;
         Debug.Log("This3");
         Debug.Log(Health);
         if(HealthS.IsHead == true) {
             Debug.Log("Hit something!");
             Health -= Damage;
         }
         else if(HealthS.IsTorso == true) {
             Debug.Log("Hit something!");
             Health -= Damage / 2;
         }
         if(HealthS.IsArm == true) {
             Health -= Damage / 3;
         }
         if(HealthS.IsLeg == true) {
             Health -= Damage / 2.5;
         }
         if(HealthS.IsOther == true) {
             Health -= Damage / 2;
         }
         Debug.Log(Health);
     }
     else {
         Debug.Log(other.collider.name + " Does not have HealthSystem script attached! ");
     }
     
     //if(hit.gameObject.tag != "Metal" && hit.gameObject.tag != "Wood" && hit.gameObject.tag != "Stone" && hit.gameObject.tag != "Dirt") {
     
     //}
     
     Destroy (gameObject);
     }
Comment
Add comment · Show 3
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 perchik · Sep 06, 2013 at 01:53 AM 0
Share

Can you post the declaration of Health (from this script)

avatar image Doneyes · Sep 06, 2013 at 01:55 AM 0
Share

Which part? The health being identified or the health being created in the other script that this script is detecting?

avatar image perchik · Sep 06, 2013 at 02:05 AM 0
Share

well I was curious about the local variable, because of exactly what @Vio$$anonymous$$yma said, for some reason I was thinking there is some way to get a reference to it, ins$$anonymous$$d of a local variable. Anywho, you'r problem's solved

1 Reply

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

Answer by VioKyma · Sep 06, 2013 at 01:55 AM

You are assigning the health value to a local variable, which you will need to assign back to the object/component you got it from.

So just make sure you assign it back after changing. (You could also just directly assign).

For example: HealthS.Central.GetComponent(CentralHealth).Health = Health;

Placed where the Debug.Log is (line 29 in your script).

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 Doneyes · Sep 06, 2013 at 02:00 AM 0
Share

Thanks! I forget the logic used in javascript because I'm so used to lua.

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

17 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

Related Questions

Debug log doesn't always activate? 1 Answer

What's the difference between print() and Debug.Log() 1 Answer

How do you get console messages in order? 0 Answers

Debug.log,print not working when run from mobile android 0 Answers

A node in a childnode? 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