- Home /
why is my GUI script bugging other GUI scripts?
so, i have many different GUI scripts attatched to my player object, one of which makes the player shoot enemies. So, i kinda figured out a way to identify the enemy being shot and display the health bar of THAT particular gameObject, but this particularly bugs my other GUI (crosshair, own hp bar etc.) in the initial phase, after which things get normal.
heres the script, please help::
using UnityEngine;
using System.Collections;
public class hit : MonoBehaviour {
private RaycastHit shot = new RaycastHit();
public GameObject enemy;
public enemyHp enemyhp;
void OnGUI ()
{
GUI.Box (new Rect(420,10, enemyhp.curHealth/enemyhp.maxHealth*300, 20),"Enemy Health");
}
void Start ()
{
}
void Update ()
{
Vector3 dir = transform.TransformDirection(Vector3.forward);
Vector3 pos1 = gameObject.transform.position;
if (Input.GetButtonUp ("Fire1"))
{
if (Physics.Raycast(pos1, dir, out shot, 20f))
{
enemyhp = shot.collider.gameObject.GetComponent<enemyHp> ();
enemyhp.curHealth = enemyhp.curHealth - 10;
}
}
}
}
thanks all in advance :3
I had a health bar, and then above it ammo amount. Looked ok in editor, building made the ammo gui disappear,
i found that putting calls to gui in several scripts attached to the same object causes major issues. i would try and put all gui in one place on a given object. Prolly work then.
You can however, have gui calls on different objects and no problems occur.
I don't understand your meaning by "bugs my other GUI". How exactly does it bug your GUIs.
the other boxes and buttons start blinking randomly and change positions this problem fixed after a few seconds
Does it happen every time? If you disable this script does the issue go away?
Can this ever return negative?
enemyhp.curHealth/enemyhp.maxHealth*300
Just out of curiosity try in line 24
if (Input.GetButton ("Fire1"))
ins$$anonymous$$d of
if (Input.GetButtonUp ("Fire1"))
Your answer
Follow this Question
Related Questions
C# Applying Transparency to a Single GUI.Button 1 Answer
C# Boolean Doesn't Change Value 1 Answer
Not working gui buttons 1 Answer
What is frame, How OnGuI is called every frame? 2 Answers
How do i change the GUI.Box font size and box size ? 0 Answers