Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 spilat12 · Jul 20, 2018 at 05:03 PM · buttoneventuser interfaceeventsonclick

OnClick() problem with Button script: "Deleting an array element will copy the complete array to all..."

I have six UI buttons with the default OnClick() behaviour. Each button has 5 callbacks which are invoked when each button is pressed, accordingly. Here is the screenshot of 4 buttons next to each other to demonstrate what I am talking about:

alt text

As you can see, right now 4 of the callbacks are the same, but with different values passed. The 5th callback is identical for all the buttons.

What I would like to do is to remove the showBoxUI.SetAnimationTo from each button. I would do it by selecting all 6 buttons, clicking the 2nd element and then pressing the minus button to remove it. And while it removes it well from all 6 buttons, I am prompted with the following message:

Deleting an array element will copy the complete array to all... Unique values in the different selected objects will be lost

And when I press the "Delete" button, the 2nd element is removed from all the buttons, BUT all the different values on each button are set to the same value!

alt text

Why does that happen and is there a way of preventing it? How do I deal with that? Obviously, I don't want to re-type each text every time I remove part of functionality from the buttons...

Thank you in advance.

btn-examples.png (119.1 kB)
btn-examples-2.png (318.5 kB)
Comment
Add comment
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

1 Reply

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

Answer by troien · Jul 21, 2018 at 02:35 PM

The reason why this happens is because the way Unity handles editing multiple objects.

For instance, when you have a script with a string variable. You put that script on 3 different GameObjects and give them each a different value. Then you select all 3 GameObjects (now you are 'editing multiple objects') and when you modify the string. The result is that all strings will be modified.

Now with this array. When 'editing multiple objects', Unity doesn't look at each array item individually, but at the complete array as a whole. So if you then multi edit one item of the array, you are actually modifying the complete array. And therefore, sinse you are multi editing, you are multi editing all arrays.


I think you might be able to overcome this issue by writing a custom instector script. But this is tricky, sinse for instance you have the following setup:

  • 1 array of length 2

  • 1 array of length 2

  • 1 array of length 6

And now you want to modify the item at index 5 of the third array. But the other 2 don't have this item at a different index, should they:

  • add it?

  • add index 2 till 5?

  • Modify their own index 1 (as that is their last )?

  • or just copy the entire array as Unity does?

As you can imagine, there is no 'good' solution in all situations. So therefore unity chooses to modify the array as a whole which I think is the best approach.

Also, sinse you are using the builtin Unity button and not a custom class for which you could be able to build a custom editor, I think you best (and easiest) option would be to simply not multi edit (i.e. remove the second item from one button at a time)...


Another (ugly) workaround could be something like this...

 public class SomeExtraFunctionality : MonoBehaviour
 {
     public UnityEvent myEvent;
 
     public void Start()
     {
         GetComponent<Button>().onClick.AddListener(myEvent.Invoke);
     }
 }

If you add this script to the button, even though the event on this script has the same issue. If you add 'item 2' to this script and the other items to the button, you could multi edit it without modifying the array on the button. Not really a solution to the problem, but depending on your situation it might be helpfull...

Comment
Add comment · Show 1 · 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 spilat12 · Jul 21, 2018 at 11:46 PM 0
Share

Thank you for a lengthy and good explanation!

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

106 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

Related Questions

Button doesn't work when returning to scene (android) 1 Answer

How to access the OnClick event via Script 1 Answer

UI Button OnClick Function - How to get name of button that was clicked? 2 Answers

How to call an Event trigger on a button through script? 1 Answer

Check if a listener has already been added to a button? 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