- Home /
Is there a way to have 1 Button update the values of variables of a script that is on multiple game objects?
So a simple example of what I want is:
I have 2 game objects with the same script with the same int value of 1 and I want to press a button to increase this int value for each object separately. An easy way would be having one button connected to each object. But let's say I have maybe 100 objects, then I'll also end up with 100 buttons. So I'm just wondering if there's a way to have 1 button that can use the data from the game object that I click on.
"use the data from the game object that I click on." Do you mean in the editor? If so, yes; you can use Selection.gameObjects
to reference all the selected ojects. Then use GetComponent, on each, to get your particular script and it's variable.
If you want them all, to always share the same VALUE, I suggest you, ins$$anonymous$$d, consider making the variable static
.
I want it so when I'm playing the game, I click on a specific game object and then when I click on the button, it increments the value of that specific game object. I'm not quite sure how to use "Selection.gameObjects" or what it does exactly either.
Answer by ahstein · Jun 25, 2018 at 11:34 PM
I'm not 100% sure what behavior you're looking for, but it seems like you need to add an element to your button script that gets a reference to the gameObject that you want to update.
For example: Option 1) User interaction-- click on your button, then click on the object you want to increment. (One approach to doing this would be to start a coroutine when you click on the button that checks whether you clicked on an object each frame, and increment that object when you do).
Option 2) User interaction-- select an object, then click the button to increment (add a "selected" field to each object. When you click the button, search through all the objects to find the selected one, and increment that one).
Option 3) User interaction-- when you click the button, increment the variable on all objects (change the variable you want to increase into a static variable)
Option 2 seems like the one I want. What do you mean by selected field though?
Thinking about it more, I would probably do it like this.
Add something like this to your object script (your object needs to have a collider for On$$anonymous$$ouseDown() to work):
public class $$anonymous$$yObject : $$anonymous$$onoBehavior {
public static $$anonymous$$yObject selectedObject;
public int incrementedVariable;
void On$$anonymous$$ouseDown() {
selectedObject = this;
}
}
And this to your button:
void IncrementSelectedObject() {
if($$anonymous$$yObject.selectedObject == null) {
return;
}
$$anonymous$$yObject.selectedObject.incrementedVariable++;
}
Then have the button trigger IncrementSelectedObject().
This worked, thanks! Just for future reference, can you explain how the static $$anonymous$$yObject works?
Your answer
![](https://koobas.hobune.stream/wayback/20220612165452im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Button - get reference to the parameter passed to function under OnClick() 1 Answer
[SOLVE] OnMouseEnter not working on UI elements 2 Answers
UI Buttons to work continuously when pressed and held for sometime 1 Answer
Pressing space calls the wrong function 1 Answer
How to get an array of all Buttons attached to a Panel? 2 Answers