Mob's health doesnt update.

When i 'm attacking the mob it’s health bar doesnt update.In same time he’s attacking me , my healthbar is updating properly.

Mob’s health update after i select it again with TAB.

Here’s my vitalbars class.

/// <summary>
/// VitalBar.cs
/// This class is resposible for displaying a vital for the palyer or a

mob.
///
using UnityEngine;
using System.Collections;

public class VitalBar : MonoBehaviour {
	public bool _isPlayerHealthBar;  //tells if its ours hp bar or mob's

hpbar.

	private int _maxBarLength;		//max length of bar when hp is 100%
	private int _curBarLength;		//current length of hp bar.
	private GUITexture _display;
	
	
	
	
	void Awake(){
	_display = gameObject.GetComponent<GUITexture>();


	}

	// Use this for initialization
	void Start () {
	//_isPlayerHealthBar = true;
	
		
		_maxBarLength = (int)_display.pixelInset.width;
		OnEnable();
	}
	
	// Update is called once per frame
	void Update () {

	
	}
	
	//This method is called when the gameobject is enabled.
	public void OnEnable(){
		if(_isPlayerHealthBar){
		Messenger<int, int>.AddListener("player health

update", OnChangeHealthBarSize);

		}
			else{
			ToggleDisplay(false);
			Messenger<int, int>.AddListener("mob health update",

OnChangeHealthBarSize);
Messenger.AddListener(“show
mob vitalbars”, ToggleDisplay);

		}
	}
		//This method is called when the gameobject is disabled.

	public void OnDisable(){
		if(_isPlayerHealthBar)
		Messenger<int, int>.RemoveListener("player health

update", OnChangeHealthBarSize);
else {
Messenger<int, int>.RemoveListener(“player health
update”, OnChangeHealthBarSize);
Messenger.RemoveListener(“show
mob vitalbars”, ToggleDisplay);

		}
		
	}
	//calculating the total size of the hpbar in relaion to the % of

health the target had left.
public void OnChangeHealthBarSize(int curHealth,
int maxHealth){
//Debug.Log(“we heard:curHealth = " + curHealth + " -maxHealth =” + maxHealth);
_curBarLength = (int)((curHealth / (float)maxHealth) * _maxBarLength);
//_display.pixelInset = new Rect(_display.pixelInset.x,
_display.pixelInset.y, _curBarLength, _display.pixelInset.height);
_display.pixelInset = CalculatePosition();
}
//setting the hpbar to the player or mob.
public void SetPlayerHealth(bool b){
_isPlayerHealthBar = b;
}

		private Rect CalculatePosition(){
		float yPos = _display.pixelInset.y /2 -10;
		
		if(!_isPlayerHealthBar){
		float xPos = (_maxBarLength - _curBarLength) - (_maxBarLength /4+10);

			return new Rect(xPos, yPos, _curBarLength, _display.pixelInset.height);
		}
		return new Rect(_display.pixelInset.x, yPos,

_curBarLength, _display.pixelInset.height);
}
private void ToggleDisplay(bool show){
_display.enabled = show;
}

}

3 Answers

3

See if you can find the instructions, from where you got this. It might be helpful if
you instead asked “XYZ script from asset store – won’t redo bar size.” But, if you look:

Messenger<int, int>.AddListener("mob health update", OnChangeHealthBarSize);

That’s telling some custom class named Messenger to check for “mod health update” being sent, and to only recompute the health bars when that happens. You probably aren’t sending that signal when you hit the mob.

Take a look at clicking on the mob and see what it does to redo the bar.

I have still no idea how to fix this. I think there might be problem in mob class Update.
Mob.cs

using UnityEngine;
using System.Collections;

public class Mob : BaseCharacter {
	private int curHealth = 100;
	private int maxHealth = 100;
	
	public GameObject chestPrefab;
	
	// Use this for initialization
	void Start () {
	//	GetPrimaryAttribute((int)AttributeName.Constitution).BaseValue = 100;
	//	GetVital((int)VitalName.Health).Update();
		
		Name = "Slug Mob";
	
	}
	
	void Awake(){
		base.Awake();
			AddjustCurrentHealth(0);
	}
	// Update is called once per frame
	
	public void Update () {




	}
		public void DisplayHealth(){
	
	Messenger<int, int>.Broadcast("mob health update", curHealth, maxHealth);
		//		Messenger<bool>.Broadcast("show mob vitalbars", false);
		
	}

	
		public void AddjustCurrentHealth(int adj)
	{
		curHealth +=adj;
		
		if (curHealth < 0)
		    curHealth = 0;
		    
		    if (curHealth > maxHealth)
		    curHealth = maxHealth;
		    
		    if(maxHealth < 1)
		    maxHealth = 1;
		
		if(curHealth == 0)
			Dead();
			
		    
		
	}
	public void Dead()
	{      
		PlayerAttack pa = (PlayerAttack)FindObjectOfType(typeof(PlayerAttack));	
		pa.targets.Remove(pa.target);
		 Debug.LogWarning("Liczba mobów: " + pa.targets.Count);
		if (pa.targets.Count > 0)
		{
			pa.target = pa.targets[0];	
		}
		Destroy(transform.parent.gameObject);
		Instantiate(chestPrefab, transform.position, transform.rotation);
		Messenger<bool>.Broadcast("show mob vitalbars", false);
	}
}

You might need my attack class as well.

private void Attack(){
	 animation.CrossFade("AxeSlash");
	float distance = Vector3.Distance(target.transform.position, transform.position);
	Vector3 dir = (target.transform.position - transform.position).normalized;
	float direction = Vector3.Dot(dir, transform.forward);
	if(distance < 3.5f){
		if (direction > 0){
		Mob eh =(Mob)target.GetComponent("Mob");
		eh.AddjustCurrentHealth(-10);
				
			


		                      
}

public void Target()
{
	Transform name = target.FindChild("Name");
	if(name == null){
		Debug.LogError("Couldnt find the Name on " + target.name);
		return;
	}
		Messenger<bool>.Broadcast("show mob vitalbars", true);
	
	name.GetComponent<TextMesh>().text = target.GetComponent<Mob>().Name;
	name.GetComponent<MeshRenderer>().enabled = true;
	
	target.GetComponent<Mob>().DisplayHealth();
	
	

}