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 Sambeat · Jul 02, 2015 at 11:41 AM · bugvariablebeginner

Help: My variable is always resetting to default value during play mode.

I have a simple script to keep track of a public HP variable and it also has a function Apply1Damage() that reduces HP by 1 everytime it is called. I also have a script which SendMessage() when a collision or a trigger is detected (OnTriggerEnter, OnCollisionEnter). I have applied the HP script to my enemies object and it works perfectly. However, when I apply the same script to my player object, the HP variable keep resetting to the initial value at a very fast speed. Using Debug.Log, I see that my player is receiving the damage and that the health variable is being reduced, but never less than the initial value-1. What could be causing this?

Here are the relevant scripts:

 public class DamageableMonster : MonoBehaviour {
 
     public float health;
     Animator damageableObjectAnimator;
 
     void Start()
     {
         damageableObjectAnimator = GetComponent<Animator>();
     }
 
     void apply1Damage()
     {
         health--;
         Debug.Log("Health down to: " + health);
     }
 
     void Update () {
         if (health <= 0)
         {
             damageableObjectAnimator.SetTrigger("Death");
         }
     }
 }


And

 public class Apply1Damage : MonoBehaviour {
 
     public float knockback;
     // Use this for initialization
     void Start () {
         knockback = 1000;
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.layer != 8)
         {
             Debug.Log("HIT");
             float xDistance = (other.transform.position.x - gameObject.GetComponentInParent<Transform>().position.x);
             float yDistance = (other.transform.position.y - gameObject.GetComponentInParent<Transform>().position.y);
             other.collider.SendMessage("apply1Damage");
             other.rigidbody.AddForce(new Vector2(xDistance * knockback, yDistance * knockback));
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.layer != 8)
         {
             Debug.Log("HIT");
             float xDistance = (other.transform.position.x - gameObject.GetComponentInParent<Transform>().position.x);
             float yDistance = (other.transform.position.y - gameObject.GetComponentInParent<Transform>().position.y);
             other.SendMessage("apply1Damage");
             other.GetComponent<Rigidbody2D>().AddForce(new Vector2(xDistance * knockback, yDistance * knockback));
         }
     }
     
 }


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 NoseKills · Jul 02, 2015 at 11:41 AM 0
Share

What could be causing this?

About a million things. Show us your code and we might be able to narrow it down to a 10 ;)

avatar image Ibzy · Jul 02, 2015 at 12:09 PM 0
Share

What Nose said - it sounds as though the health is being reset in Update() though?

avatar image Pangamini · Jul 02, 2015 at 12:10 PM 0
Share

Learn how to use debugger and breakpoints. It will help you very much

avatar image Sambeat · Jul 02, 2015 at 02:51 PM 0
Share

I edited my original post and added the 2 scripts. As I said, it work fine with ennemies being hit, but not with the player being hit, for some reason..

avatar image $$anonymous$$ · Jul 02, 2015 at 06:10 PM 0
Share

What about the layers? Is the player in layer 8? Does the log print the "Health down to: ..."?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Coffee-with-Venky · Jul 03, 2015 at 07:29 AM

if you are trying to decrease health when gameobject,layer 8 ...........!!! then use like this

public class Apply1Damage : MonoBehaviour {

  void OnTriggerEnter2D(Collider2D other)
  {
     if (other.gameObject.layer ==8) 
      { 
        // if gameobject triggered with layer 8 it will work 
        other.SendMessage("apply1Damage");
       }
  }

//finds with name !!!

void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.name=="Enemy") { other.SendMessage("apply1Damage"); } }

}

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 KevinCodes4Food · Jul 02, 2015 at 07:12 PM

Check your scene to see if any object is colliding with your player.

Change

 Debug.Log("HIT");

to

 Debug.Log(name + " was HIT by " + other.name);

To find out what is colliding with your player. If may be the ground, an attached equipment item, etc.

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

7 People are following this question.

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

Related Questions

How to change variable from another script 2 Answers

Changing variable values not having any effect 2 Answers

C# script for calculating the distance between the player and objects 1 Answer

In order to call GetTransformInfoExecectUpToDate 1 Answer

Why isn't the variable being assigned? 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