CyberScope _
CyberScope _

Reputation: 17

How can I stop floats being added repeatedly and making really big numbers but still wanting the code to update the floats

Basically, I am having issues with my code for dealing damage based on which body part (or collider) it hit. It works fine except for the part in the player object (the parent object containing all the limbs and body parts) where I want it to add all the separate healths of the body parts and it together into one float called currentHealth. The adding part works also completely fine its just it continuously adds the numbers as it is in the update. I have tried some solutions but realized that I need the floats to be added in the update as I need the float to change as it takes damage. Another thing to note is that the take damage function is on the separate limb health scripts meaning I cannot put the float adding thing in the take damage function for my player. Below I have pasted the code that I am talking about.

currentHealth = head.currentHeadHealth += body.currentBodyHealth += lArm.currentLeftArmHealth += rArm.currentRightArmHealth += lHand.currentLeftHandHealth += rHand.currentRightHandHealth += lLeg.currentLeftLegHealth += rLeg.currentRightLegHealth += lFoot.currentLeftFootHealth += rFoot.currentRightFootHealth; 

(the code above is in the update function) all the floats called current?health are from each of the scripts which are referenced at the top of the script.

    public float playerHealth;
    public float currentHealth;
    public healthBar healthBar;
    public TextMeshProUGUI hpAmount;
    public headHealth head;
    public bodyHealth body;
    public Leftarmhealth lArm;
    public Rightarmhealth rArm;
    public Lefthandhealth lHand;
    public Righthandhealth rHand;
    public Leftleghealth lLeg;
    public Rightleghealth rLeg;
    public Leftfoothealth lFoot;
    public Rightfoothealth rFoot;

edit: Forgot to say (sorry my fault) but the reason I have different scripts is because each limb ie head or left leg each have special scripts like the head only has a hinge joint 2d which I want to disable when the currentHeadHealth reaches 0 whereas the right Arm needs to disable the hinge joint, the arm controller script and the hand controller script for the connected hand. This is the reason for all the separate scripts as each ones have different components that need to be disabled

Upvotes: 0

Views: 59

Answers (2)

B3NII
B3NII

Reputation: 415

Well, first I think its a little messy. I don't see your project but I can give you a suggestion: don't create separate scripts for the same goal. If all your parts have a unique script, that might be unnecessary. (Ignore this if the body part fields are not objects somehow.)

So my advice is, instead of creating separate classes for the same reason (storing health and taking damage), create only one and then store it in a list on the parent object.

Something like this:

public class BodyPartHealth
{
   public float health = 10f;

   public void ReceiveDamage(float damage)
   {
       health -= damage;
       health = Mathf.Clamp(health, 0, 10f);
   }
}

Then the parent script could look like this:

public class ParentScript
{
   public List<BodyPartHealth> bodyParts = new List<BodyPartHealth>();
   public float playerHealth;
   public float currentHealth;

   public void Update()
   {
       currentHealth = 0;
       foreach(BodyPartHealth part in bodyParts)
       {
           currentHealth += part.health;
       }
   }
}

This will make sure that your code is easily readable and can be managed quickly.

Attach the BodyPartHealth script to your body parts and then add the parts to the parent list in the inspector.

Upvotes: 1

derHugo
derHugo

Reputation: 90724

I guess you wanted to just use + instead of +=.


x += y

basically means

x = x + y 

so you are telling each of your float fields to be increased by the value after them at the same time as you are building the sum.

As a shorter example of what happens

float a = 1;
float b = 2;
float c = 3;

var x = a += b += c;

will result in

a : 6
b : 5
c : 3

x : 6

Why? Because what happens internally is the following

b = b + c; // 2 + 3 -> 5
a = a + b; // 1 + 5 -> 6
x = a;

You probably rather just wanted to do

currentHealth = head.currentHeadHealth 
                + body.currentBodyHealth 
                + lArm.currentLeftArmHealth 
                + rArm.currentRightArmHealth 
                + lHand.currentLeftHandHealth 
                + rHand.currentRightHandHealth 
                + lLeg.currentLeftLegHealth 
                + rLeg.currentRightLegHealth 
                + lFoot.currentLeftFootHealth 
                + rFoot.currentRightFootHealth;   

Upvotes: 4

Related Questions