- Home /
Question by
kariemhassan2009 · Feb 13 at 03:38 PM ·
multiplayerphotonupdate functionhealthbarupdate problem
update method isnt working
so my update method isnt working / its not running her is my script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
public class Health : MonoBehaviourPunCallbacks, IPunObservable
{
public bool canDie = true;
public int maxHealth = 100;
public int currentHealth = 100;
public Transform transform;
public Image Healthbar;
public bool replaceWhenDead = false;
public GameObject deadReplacement;
public bool makeExplosion = false;
public GameObject explosion;
public bool isPlayer = false;
public GameObject deathCam;
private bool dead = false;
public Text healthtext;
float lerpSpeed;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(currentHealth);
}
else if (stream.IsReading)
{
currentHealth = (int)stream.ReceiveNext();
}
}
void Start()
{
// Initialize the currentHealth variable to the value specified by the user in startingHealth
currentHealth = maxHealth;
}
void FixedUpdate()
{
healthtext.text = currentHealth + "%";
lerpSpeed = 3f * Time.deltaTime;
HealthBarFiller();
colorChanger();
}
void HealthBarFiller()
{
Healthbar.fillAmount = Mathf.Lerp(Healthbar.fillAmount, currentHealth/maxHealth, lerpSpeed);
}
void colorChanger()
{
Color healthColor = Color.Lerp(Color.red, Color.green, (currentHealth / maxHealth));
Healthbar.color = healthColor;
}
public void ChangeHealth(int amount)
{
// Change the health by the amount specified in the amount variable
currentHealth += amount;
// If the health runs out, then Die.
if (currentHealth <= 0 && !dead && canDie)
Die();
// Make sure that the health never exceeds the maximum health
else if (currentHealth > maxHealth)
currentHealth = maxHealth;
}
IEnumerator Respawn()
{
transform.Find("Pm").GetComponent<Camera> ().enabled = false;
transform.position = new Vector3(-3, 1, 19);
yield return new WaitForSeconds(4);
transform.Find("Pm").GetComponent<Camera> ().enabled = true;
currentHealth = maxHealth;
dead = false;
}
public void Die()
{
// This GameObject is officially dead. This is used to make sure the Die() function isn't called again
dead = true;
// Make death effects
if (replaceWhenDead)
Instantiate(deadReplacement, transform.position, transform.rotation);
if (makeExplosion)
Instantiate(explosion, transform.position, transform.rotation);
if (isPlayer && deathCam != null)
deathCam.SetActive(true);
// Remove this GameObject from the scene
if(currentHealth <= 0)
{
StartCoroutine(Respawn());
}
}
}
Comment