0
\$\begingroup\$

I have my health (script) component on my player GameObject that gets boosted whenever we tap on that GameObject. However, I want to add a separate GUI texture button GameObject and tap on that to boost my health.

How do I access the health script with this new button button to get it working? I actually want to test this method for tapping on my health packs to increase the health.

This is the existing script I want to access:

//this is my health bar script which increases by tapping
var health = 300;

function OnGUI(){
    if(GUI.Button(Rect(10,10,health,10), "")){
        health += 25;
    }
}
\$\endgroup\$
1
\$\begingroup\$

Your new script will need to reference the gameobject that holds your health script, or directly reference your health script from your new script.

For example, I would modify your existing health script (I'm going to write it in C#, but you can convert it to UnityScript if you prefer):

//this is my health bar script which increases by tapping
int health = 300;

void OnGUI(){
    if(GUI.Button(Rect(10,10,health,10), "")){
        AddHealth();
    }
}

public void AddHealth() {
    health += 25;
}

Then in your other gameobject script you can write something like:

public GameObject healthObject;
void Start() {
   healthObject = GameObject.Find("NameOfYourHealthGameObject");
}


void OnGUI(){
    if(GUI.Button(Rect(10,10,health,10), "")){
        healthObject.GetComponent<NameOfYourHealthScript>().AddHealth();
    }
}

You have lots of options here. You can also assign the object manually by dragging and dropping the health GameObject onto the new script's healthObject field in the inspector (if you do that remove the code in the Start method). Or you could use the event system, or lots of other options.

I've also noticed you're using the old OnGUI method of creating your GUI. I'd suggest moving to the new GUI system recently released from Unity. It's much easier to use and provides a lot of functionality for you automatically. Additionally, for someone who's new to Unity, it allows a lot more work to be done solely in the Unity editor, which means less scripting and more game making.

\$\endgroup\$
5
  • \$\begingroup\$ thanks for your help, I got it solved, but How do I reference my health script to my custom created Healthbar Ui textures? I've switched everything but my health script still contains some black bar which uses the old OnGui rect draw function, how do I make my healthbar script work with my new Image Canvas Ui healthBar textures? javascript please. \$\endgroup\$
    – isaacj11
    Feb 4 '15 at 15:36
  • \$\begingroup\$ That sounds like a different problem. You should ask a new question. \$\endgroup\$
    – House
    Feb 4 '15 at 15:53
  • \$\begingroup\$ no its not its similar please help for this one, I've got a canvas>image>healthbarfill and in component, image type set as "filled" , I found some code but I don't understand, void Update(){ Image image = GetComponent<image>(); image.fillamount = .2f; } please help me link this to my health script.. \$\endgroup\$
    – isaacj11
    Feb 4 '15 at 16:09
  • \$\begingroup\$ youtube.com/watch?v=y3OZXMxsrUI I want to link my health script with my health ui textures as shown in the video but I don't understand \$\endgroup\$
    – isaacj11
    Feb 4 '15 at 16:11
  • 1
    \$\begingroup\$ unity3d.com/learn/tutorials/modules/intermediate/… .. check out this link I would recommend completing this one beginning to end cause it has lots of great info including using nav mesh. They use ui sliders to accomplish a healthbar. Lots of goodies and it is a great example project \$\endgroup\$ Feb 4 '15 at 16:35
0
\$\begingroup\$

No problem with the new UI you can Add a button and place where you would like it. Set the image as the image you would like to use. Then you can create a OnClick event and drag your object with the script accessing the health to it. Click on the function in the onclick and set it to the function to increase the health change to something like this and you will be able to select it as a function in the Buttons OnClick

public void IncreaseHealth() 
{

        health += 25;
        Debug.Log(health);

}
\$\endgroup\$
6
  • \$\begingroup\$ Also note if you want to access it from multiple scripts you could always make it a public static health then you would access it by YourScript.health +=25. but yes there are many different ways to achieve results you are looking for \$\endgroup\$ Feb 3 '15 at 18:04
  • \$\begingroup\$ Thank you guys I was able to access it with the new Ui tools instead of OnGui, this method was much easier and simpler appreciate you guys. Ill post if I get any more doubts. thanks \$\endgroup\$
    – isaacj11
    Feb 4 '15 at 4:22
  • \$\begingroup\$ Well How do I implement this to my newly created Health Ui textures? I've switched everything but my health script still contains some black bar which uses the old OnGui rect draw function, how do I make my health script work with my new Image Canvas Ui textures? javascript please. \$\endgroup\$
    – isaacj11
    Feb 4 '15 at 15:19
  • \$\begingroup\$ first empty the code in the OnGUI() so it doesn't draw the old stuff anymore. Then click the button you want to use. In the properties it will have OnClick click the plus sign to add a avent to the button. now you can drag the gameobject that has your health script to the OnClick event ..be sure to set editor and runtime then next to it click on the function and next the object with your script. You will see your method IncreaseHealth select that and when you click it it will work \$\endgroup\$ Feb 4 '15 at 16:18
  • \$\begingroup\$ No I got it working, Im able to press the button and get the health increased, but all I want is to change that health bar to my custom UI texture but I can't \$\endgroup\$
    – isaacj11
    Feb 4 '15 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.