Using the same script for two objects and resetting income

Hello,

First issue - I’m trying to use the same script below for the Policeman and Fireman objects. For the Policeman works fine, but when I hire Fireman the salary from the Fireman doesn’t add to the total negative income up in the right corner. i don’t get any errors so I can’t tell if it’s a conflict somewhere. The second issue is that I have set a maximum of 5 hired, but if I press to hire more (even if I know it won’t) it resets the amount of negative income that increased to the time I press the hire button again. So I hire 5, the negative income goes -650, -651, -652, etc. and if I press the hire button it goes back to -650.

Thank you

public class IncrementPlusOne : MonoBehaviour
{

public TMP_Text PolicemanHiredNumberText; 
public int hired;        
public TMP_Text salaryText; 
public TMP_Text totalSalaryText; 
public float salaryAmount;   
public float salaryPerSecond; 
public int totalSalaryPerSecond; 
public float totalSalaryAmount;

public void Start()
{
   salaryAmount = -130f;
   salaryPerSecond = 1f; 
    
}

public void Update() 
{
    

    Policeman![192891-capture.jpg|1093x631](upload://oaEv5n5esCEhbGDRdIwilPOvNBP.jpeg)HiredNumberText.text = hired + "";  
    salaryText.text = (int)totalSalaryAmount + ""; 
    totalSalaryText.text = -(hired * salaryAmount) + ""; 

    totalSalaryPerSecond = (int)(salaryPerSecond * hired); 
    totalSalaryAmount -= totalSalaryPerSecond * Time.deltaTime; 

}
   
public void Hireworkers()
{

    if (hired < 5) { hired++; }  

    totalSalaryAmount = (int)(salaryAmount * hired);
 }

}

If your totalSalaryAmount represents the combined salary of every single fireman and policeman in play, then you need a different kind of value to store that. You might find some luck with using the static modifier: static documentation. static means it gets shared between all instances of the same class.

Possibly you could benefit from inheritance. If Policeman and Fireman have much of the same behaviour and variables, consider a base class Employee and have the others inherit from it.