简体   繁体   中英

unity control one object from many same object in scene that have same script c# with UI button

i have three same object with same script in scene, i also has one button for upgrade the object script's variable i selected

below is what i tried but still upgraded all of object variable instead just what i selected

what i want is when i selected one of the object on scene and clicked the button, then just selected object script variable upgraded instead all of the object script variable

public int lv;
public float hp;
public float damage;
public float atkSpeed;
public GameObject objBtnUpgrade;
public Button btnUpgrade;
void Start () {

    objBtnUpgrade = GameObject.Find ("BtnUpgrade");
    objBtnUpgrade.GetComponent<Button> ().onClick.AddListener(() => { UpgradeSummonLevel(); }); 

}
public void UpgradeSummonLevel(){
    lv = lv + 1;
    hp = hp + ((hp / 100) * 10);
    atkSpeed = atkSpeed + ((atkSpeed / 100) * 10);
    damage = damage + ((damage / 100) * 10);
}

Are you asking why every one of your objects is being upgraded?

But to answer you question as far as I understand, the most likely problem is that you have all of the objects that you are trying to influence called "BtnUpgrade". They should have differing names that way you can find a single one by its name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM