- Home /
How can I create a GUI with a variable from another script? C#
Hi! I want to make a GUI that shows my bullet damage, however, the variable (I'm using a float) of the damage is in a diferent script. I want to access my Damage float from another script.
My Scripts:
public class BulletScript : MonoBehaviour {
public float Damage = 50; void OnCollisionEnter(Collision Other){
if ( Other.gameObject.GetComponent( typeof(AIscript) ) != null && Other.gameObject != objPlayer ) // if we have hit a character and it is not the player
{
AIscript ptrScriptAI = (AIscript) Other.gameObject.GetComponent( typeof(AIscript) );
ptrScriptAI.health -= Damage;
Instantiate(ptrScriptVariable.parAlienHit, transform.position, Quaternion.identity );
removeMe();
}
I want to take the float Damage in put it in to my GUI. My other script:
public class AIscript : MonoBehaviour {
public GUISkin Heatlh; public GUISkin damage; public float health = 50f;
void OnGUI() {
if (thisIsPlayer == true)
{
GUI.skin = Heatlh;
GUI.Label (new Rect (Screen.width * (100.4f/120.55f),Screen.height * (100.1f/120.3f),Screen.width * (5f/12.55f), Screen.height * (5f/12.3f)),"Health: " + health.ToString()+ "%");
}
}
Answer by smallbit · Oct 08, 2013 at 07:52 PM
To access variable from another game object try this http://answers.unity3d.com/questions/35425/get-variable-from-a-different-game-object.html
Or you can use sendMessage to inform your gui that variable has been changed, than on receiving it in the gui script just change any local var that is diplayed in gui
Thanks for your answer. Can you please give an example of the send$$anonymous$$esage call? I don't quite know how to use it
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
for loop error 1 Answer