- Home /
Use UI Button from other Script
Hello,
actually i Try to make an Update System for my Player Objects with UI. So i have many PlayerObjects in the Scene all have Attached the PlayerManager When i select the Player Object i call
public void UpgradeUI()
{
uiFields.upgradeLife.onClick.AddListener(() => { UpgradeLife(); });
uiFields.shipNameUI.text = shipStats.shipName;
uiFields.actualLifeText.text = startHealth.ToString();
uiFields.upgradeLifeButtonText.text = lifeToAddString;
}
so the Selected PlayerObject Updates the UI public class UpgradeUI : MonoBehaviour {
[Space(5)]
[Header("UI Elements for Upgrade")]
public Canvas upgradeCanvas;
public GameObject upgradePanel;
public Text shipNameUI;
public Button upgradeLife;
public Text upgradeLifeButtonText;
public string LifeText;
public Text actualLifeText;
}
Works as fine he open the Panel he put in PlayerObjectName expect the Button he isnt working and i have no idea how to solve that problem and hope that someone of you guys can help me out with an idea or example or different way to deal with that.
here what happens in the
public void UpgradeLife()
{
//Simple everyTime is called he add Life to the restartHealth and Update the UI
restartHealth = restartHealth + lifeToAdd;
uiFields.actualLifeText.text = restartHealth.ToString();
}
Thanks for help
Answer by megabrobro · Dec 06, 2017 at 03:05 PM
You can use the Inspector area for the Button. It has OnClick method there by default (you can also add Event Trigger component to it, which gives OnPointerUp/OnPointerDown etc.)
So you make a method in your code something like:
public void SomeButtonIsClicked(){
// do something when the user clicks the button
// for example:
UpgradeUI();
}
and then in the Inpsector, you drag that class onto the OnClick function and it has a box to select which method from that class should be called when a OnClick event is received.
I couldnt fully understand your question but i think that might be the answer
Finally i got it thank you. You bring me on the right way!
When i select the Unit i also bring an Reference to the UpgradeUI of the selected Object. Then i get the Component and call the Upgrade $$anonymous$$ethod in Player$$anonymous$$anager
@DerDerErIst - very glad to have helped you
Answer by DerDerErIst · Dec 06, 2017 at 03:53 PM
The problem about is that i have many Objects of the same Type and they all should Access to that 1 Canvas, so every time i open the Canvas i open it up from a another Object so the Values in that Canvas change, but i only can declare a Object to OnClick. If i insantiate another kind of that Object he use the same Reference like the old because the Button not change. It always change the same Object in Start.
Or i need to load for every Object a own Canvas Panel? Im not sure im relative new to Programming and All.
Also Forgive for my English.
you're English is a lot better than me for any other language! But back to your problem, I beleive you can fix that easily. The method in my example can be called the same each time from the UI Button. But inside the method it needs to specify which object you want to interact with. You will need to make a variable in the Player$$anonymous$$anager class which will store a reference to current object that the button is supposed to interactive with
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to check if a public method is referenced in inspector 3 Answers