Unity-2D learning [BUG record] character blood bar and hit problem

Requirement: Create a blood bar UI that deducts blood when the character is hit.

Idea: Create a canvas, create BaseUI>HP>TEXT in order, write HP.cs, and initialize the current value HealthCurrent and the maximum value (original value) HealthMax two variables, and obtain the character's original value health variable from PlayerController for assignment.

Code (HP.cs):

...
    // 初始化数显 
    public Text healthText;
    // 定义静态变量当前血量
    public static int HealthCurrent;
    // 定义静态变量原始血量
    public static int HealthMax;

    private Image healthBar;
    // Start is called before the first frame update
    void Start()
    {
        healthBar = GetComponent<Image>();
        HealthCurrent = HealthMax;
    }

    // Update is called once per frame
    void Update()
    {
        healthBar.fillAmount = (float)HealthCurrent / (float)HealthMax;
        healthText.text = HealthCurrent.ToString() + "/" +HealthMax.ToString();
    }

Code (PlayerController.cs):

...
    // 定义角色血量
    public int health;
    // Start is called before the first frame update
    private void Start()
    {

       ...
        HealthUI.HealthMax = health;
    }

...

Run it and see:

There is a BUG: HP is not initialized after running, or the initialization fails, and it does not show the full blood status.

 After being beaten, it returns to normal, which means that the Update function runs successfully and can change the change and ratio of the blood bar.

I think this is a logical problem. First of all, the blood volume UI should be attached to the player. The character and script of the player should be initialized at the beginning of the game. After the initialization is completed, the blood volume UI and other auxiliary components should be available. Go get the values ​​and components in the player, and now I know that the initialization of the player is completed and the HP is given to the static variable of HP through running, but it can be said that the Start of the UI may be initialized at the same time as the player (because both It’s the first frame, let’s think so), and it’s completed in the first frame, so when the UI is initialized, HealthCurrent = HealthMax; what you get is actually the default value, and the default value of the int variable is 0, so it displays 0 when I click Run /100, when he was hit, he called the value given by the player and returned to normal.

Violent solution: Increase the priority of the player, let him complete the initialization in advance, give the value to Hp, and then initialize the hp again, this can solve the problem, just change the Start in the player to Awake.

Recommended solution: without changing the code, click on the script file, open the project settings, click on the script execution order, and drag all the scripts that need to be executed in order into this box. The smaller the value, the faster the execution.

Method 1 Explanation: Unity is a single-threaded operation. The so-called coroutine is similar to multi-threaded, but it is actually executed by a single thread. The execution order of the functions in the unity script is as follows: Awake>OnEable>Start>FixedUpdate>Update>LateUpdate
> OnGUI>Reset>OnDisable>OnDestroy

Method 2 screenshot:

BUG has been fixed.

 

 

Guess you like

Origin blog.csdn.net/Joker6578/article/details/127954239