- Home /
Gave up yet again.
Health and ammo GUI not working on re-spawn
Hi Everyone, I am making a FPS demo and I have a problem. I have a health and ammo GUI working from the old FPS tutorial on this site, but theirs does not work when re-spawning. I have tried different ways and even tried using the unity GUI but to no effect. The GUI's I have work great when I don't die, but right when I re-spawn a character, the GUI are stuck to what they were when I died. It seems to be that the prefab does not let me add the GUIText and the GUITexture to it which is probably my problem but I don't know how to attach it to the prefab through code. If anyone can help me, I would appreciate it. Here is the code for the GUI and my re-spawning of my character:
var maximumHitPoints = 100.0;
var hitPoints = 100.0;
var prefabPlayer:Transform;
var bulletGUI : GUIText;
var healthGUI : GUITexture;
private var healthGUIWidth = 0.0;
function Awake () {
healthGUIWidth = healthGUI.pixelInset.width;
}
function Die () {
if (die)
AudioSource.PlayClipAtPoint(die, transform.position);
Destroy(gameObject);
}
function LateUpdate () {
// Update gui every frame
// We do this in late update to make sure machine guns etc. were already executed
UpdateGUI();
if (Input.GetKey("t"))
Respawn();
}
function UpdateGUI () {
// Update health gui
// The health gui is rendered using a overlay texture which is scaled down based on health
// - Calculate fraction of how much health we have left (0...1)
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
// Update machine gun gui
// Machine gun gui is simply drawn with a bullet counter text
if (machineGun) {
bulletGUI.text = machineGun.GetBulletsLeft().ToString();
}
function Respawn()
{
Instantiate(prefabPlayer, transform.position, transform.rotation);
}
Follow this Question
Related Questions
3D Health Bar - Killing My FPS 1 Answer
Show GUI only when looking at an object? 1 Answer
Can't get Gui Pos on top of Enemy ingame! 0 Answers
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers
Can't move GUI 1 Answer