Hi guys so iveI've been following a guide on building a 2D MMORPG, iand I ran into a problem when creating a health bar for my players. I followed the guide step by step, didnt work, i followed the official doc HERE, changed my code and still ran into the sameget 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)
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,code; please could someone explain to me why it is giving me this error X_X?
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;
}
}
```
I would really appreciate any help, i am still a beginner so literally any advise is good advise and id absolutely love it. If you made it this far, id like to thank you for taking the time out and reading this ! Hope you enjoy the rest of your day <3 :D
King Regards Iyaad S