Julio Andryanto
Julio Andryanto

Reputation: 82

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);
}

Upvotes: 0

Views: 186

Answers (1)

Mindstormer
Mindstormer

Reputation: 294

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.

Upvotes: 1

Related Questions