using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public static GameController GC;
private UserInput player { get { return FindObjectOfType<UserInput>(); } set { player = value; } }
private PlayerUI playerUI { get { return FindObjectOfType<PlayerUI>(); } set { playerUI = value; } }
private WeaponHandler wp { get { return player.GetComponent<WeaponHandler>(); } set { wp = value; } }
private CharacterStats stats { get { return player.GetComponent<CharacterStats>(); } set { stats = value; } }
void Awake()
{
if (GC == null)
{
GC = this;
}
else
{
if (GC != this)
{
Destroy(gameObject);
}
}
}
void Update()
{
UpdateUI();
}
void UpdateUI()
{
if(player)
{
if (playerUI)
{
if (wp)
{
if (wp.currentWeapon == null)
{
playerUI.ammoText.text = "Unarmed";
}
else
{
playerUI.ammoText.text = wp.currentWeapon.ammo.clipAmmo + "/" + wp.currentWeapon.ammo.maxClipAmmo + "|" + wp.currentWeapon.ammo.carryingAmmo;
}
}
if(playerUI.HealthSlider && playerUI.HealthText)
{
playerUI.HealthText = Mathf.Round(playerUI.HealthSlider * 100).ToString();
}
}
}
}
}
Looks like it should. You’re gonna have to give a bit more information about how they “won’t sync” before anyone can suggest something useful. Is it just a rounding error? Add 0.5f inside of the call to Mathf.Round() if it is.
the line 59 is error (playerUI.HealthSlider * 100) is currently red lined
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour
{
public Text ammoText;
public Slider HealthSlider;
public Text HealthText;
}
this is the playerUI
You didn’t say there was an error. You should always post the precise text of the error if you are seeking assistance.
In this case I can see you are assigning a string to a UnityEngine.UI.Text object on line 59.
You probably intend to assign it to the .text field of that object.
im still new in this things sorry already figure it out.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CharacterStats : MonoBehaviour
{
private CharacterController characterController { get { return GetComponent<CharacterController>(); } set { characterController = value; } }
private RagdollManager ragdollManager { get { return GetComponentInChildren<RagdollManager>(); } set { ragdollManager = value; } }
private PlayerUI UI { get { return GetComponent<PlayerUI>(); } set { UI = value; } }
private CharacterStats stats;
[Range(0, 100)]
public int Startinghealth = 100;
public int MaxHealth = 100;
public int faction;
public MonoBehaviour[] scriptsToDisable;
bool damage;
void Update()
{
Startinghealth = Mathf.Clamp(Startinghealth, 0, 100);
}
public void Damage(int amount)
{
Startinghealth -= amount;
if (Startinghealth <= 0)
{
Die();
}
}
void Die()
{
characterController.enabled = false;
if (scriptsToDisable.Length == 0)
{
return;
}
foreach (MonoBehaviour script in scriptsToDisable)
script.enabled = false;
if (ragdollManager != null)
ragdollManager.RagDoll();
Destroy(gameObject, Random.Range(10, 20));
}
public void HealthPack()
{
Startinghealth = MaxHealth;
}
}
but this time there is no error but my UI is not updating to my starting health and the health text just starts at 0.
Start inserting Debug.Log() statements in your code to track what the actual value is in various parts of the code. This is known as “printf debugging” and is the easiest-to-do and most versatile approach to understanding the engineering problem you have.