Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reduce two variables with each other

Discussion in 'Scripting' started by Devxdprogamer, Aug 4, 2021.

  1. Devxdprogamer

    Devxdprogamer

    Joined:
    Jun 1, 2021
    Posts:
    20
    I have a car and some enemies in my scene.
    The enemy has a script with a Health Int
    The car also Has a script with a Health Int
    When They both Collide They reduce their healths
    EnemyHealth - CarHealth
    CarHealth - EnemyHealth
    (The car Health is mostly higher than enemy health)
    I tried many ways in doing this but either the Cars health will increase instead of Decreasing
    or Enemy Health Will Increase Instead Of Decreasing

    //Enemy
    Code (CSharp):
    1.     public void TakeDamage ( int damage , bool isAlly , GameObject Damager )
    2.     {
    3.         int HealthRecord;
    4.         HealthRecord = Health;
    5.         Health = Health - damage;
    6.         Debug.Log (transform.name + Health);
    7.         if (isAlly)
    8.         {
    9.             Damager.GetComponent<Ally>().TakeDamageAlly ( HealthRecord );
    10.         }
    11.     }
    //Car
    Code (CSharp):
    1.         void OnCollisionEnter (Collision target)
    2.         {
    3.             if ( target.gameObject.tag == EnemyTag )
    4.             {
    5.                 Debug.Log ("Enemy Collision Detected" + target.gameObject.name );
    6.                 EnemyHealthRecord = target.gameObject.GetComponent<Enemy>().Health;
    7.                 target.gameObject.GetComponent<Enemy>().TakeDamage( AllyHealth , true , this.gameObject );
    8.                 AllyHealth -= EnemyHealthRecord;
    9.                 StartAllyHealth = AllyHealth;
    10.             }
    11.         }
    12.  
    13.     public void TakeDamageAlly ( int damage )
    14.     {
    15.          AllyHealth -= damage;      
    16.     }
    The Health Variables are just public in the script and Declared in the inspector
    Like
    Code (CSharp):
    1. public int Health;
    I couldn't get this to work
    Thanks in Advance,
    Dev
     
  2. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Have you tried stepping through your code with the debugger?
     
  3. Devxdprogamer

    Devxdprogamer

    Joined:
    Jun 1, 2021
    Posts:
    20
    What is the Debugger???
    I Don't Know What it is
    Do you find anything wrong with my code???
     
  4. Devxdprogamer

    Devxdprogamer

    Joined:
    Jun 1, 2021
    Posts:
    20
    I got it Working
    Here is the Code that worked BTW
    Code (CSharp):
    1.         void OnCollisionEnter (Collision target)
    2.         {
    3.             int EnemyHealthTemp;
    4.             int AllyHealthTemp;
    5.  
    6.             if ( target.gameObject.tag == EnemyTag )
    7.             {
    8.  
    9.                 Debug.Log ("Enemy Collision Detected" + target.gameObject.name );
    10.                 EnemyHealthTemp = target.gameObject.GetComponent<Enemy>().Health;
    11.                 AllyHealthTemp = AllyHealth;
    12.                 target.gameObject.GetComponent<Enemy>().Health -= AllyHealthTemp;
    13.                 AllyHealth -= EnemyHealthTemp;
    14.             }
    15.         }
    Thanks to everyone who replied
     
  5. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    Highly recommend learning about break points and attaching the debugger. It's an invaluable to for your arsenal!
     
    Lekret likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,525
    You can find some info here but there's also plenty of online videos about it too.

    Good luck.