I've been following a guide on building a 2D MMORPG, and I ran into a problem when creating a health bar for my players. I get this error message:
Could not find hook for 'CurrentHealth', hook name 'OnChangeHealth'. Method signature should be void OnChangeHealth(System.Int32 oldValue, System.Int32 newValue) (at System.Int32 Player::CurrentHealth)
Here is my code; please could someone explain to me why it is giving me this error?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using Mirror;
public class Player : NetworkBehaviour
{
// We have an invetory system
// We have action abilities, interact wit the world
// Our stats and skill levels are here
// Character speed variable
public float speed;
// Character Health variable | Sync Var syncs all health across the server
public const int MaxHealth = 100;
//Detects when a health change happens and calls the appropriate function
[SyncVar(hook = "OnChangeHealth")]
public int CurrentHealth = MaxHealth;
public RectTransform healthBar;
//Decrease the "health" of the GameObject
public void TakeDamage(int amount)
{
if (!isServer)
return;
//Decrease the "health" of the GameObject
CurrentHealth -= amount;
//Make sure the health doesn't go below 0
if (CurrentHealth <= 0)
{
CurrentHealth = 0;
}
}
void Update()
{
// If the player is a (client)
if (isLocalPlayer)
{
// Controls player movements
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime);
transform.Translate(Vector3.up * Input.GetAxis("Vertical") * speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
CmdTakeHealth();
}
}
}
// Updates healthBar
void OnChangeHealth(int health)
{
healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y);
}
//This is a Network command, so the damage is done to the relevant GameObject
[Command]
void CmdTakeHealth()
{
//Apply damage to the GameObject
TakeDamage(2);
}
public override void OnStartLocalPlayer()
{
// When a new player joins, run this code for the new client
GetComponent<Renderer>().material.color = Color.blue;
}
}
```