Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NekoUltima · Jun 25, 2018 at 11:07 PM · scripting problemuibutton

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Jun 25, 2018 at 11:18 PM 0
Share

"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.

avatar image NekoUltima Glurth · Jun 25, 2018 at 11:47 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NekoUltima · Jun 25, 2018 at 11:42 PM 0
Share

Option 2 seems like the one I want. What do you mean by selected field though?

avatar image ahstein NekoUltima · Jun 25, 2018 at 11:56 PM 1
Share

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().

avatar image NekoUltima ahstein · Jun 26, 2018 at 12:24 AM 0
Share

This worked, thanks! Just for future reference, can you explain how the static $$anonymous$$yObject works?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges