Program does action two times

Viewed 58

I am working on a project in Unity. I made some code that gives you in game money every second but when i play my project, there is a bug that gives you money two times. How can i fix this. Maybe it has to do with the static variables i use but i would like help. Thank you.

this is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStats : MonoBehaviour
{
    [SerializeField]
    public static int soldiers;
    [SerializeField]
    public static int money;
    [SerializeField]
    public static int income;

    private int nextUpdate = 1;

    // Start is called before the first frame update
    void Start()
    {
        income = 500;
        money = 2500;
        soldiers = 20;

    }

    // Update is called once per frame
    void Update()
    {
        if(Time.time >= nextUpdate)
        {
            nextUpdate = Mathf.FloorToInt(Time.time) + 1;
            UpdateEverySecond();
        }
    }

    void UpdateEverySecond()
    {
        GetMoney();
        DecreaseIncome();
    }


    void GetMoney()
    {
        money += income;
    }

    void DecreaseIncome()
    {
        if (income >= 0)
        {
            income -= 1;
        }
    }
}
1 Answers

The problem was in another script that used PlayerStats instead of monobehavior and somehow it just dubled everything that the class PlayerStats did.

Related