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 /
  • Help Room /
avatar image
0
Question by VizuaaLOG · Aug 21, 2017 at 08:04 PM · prefabbuttonuser interfaceonclick

Button prefab is causing wrong prefab to trigger

Okay, so I'm having a very weird issue and it is causing me to pull my hair out! Have spent hours trying to work this out.

So, I have a prefab called 'ShopItem' this prefab is a UI button with four child objects being UI image or a UI panel. When the prefab is created it gets a listener attached to the on click method. The click handler works, but it seems to only trigger whatever the last instance in the hierarchy.

alt textalt text

Above you can see the two objects when the game is running, with the game hierarchy shown. These two objects were instantiated using the below script (ignore the messy code this just a test):

 void Start() {
     var item = Instantiate(ShopItemPrefab);
     item.name = "Shop Item 1";
     item.GetComponent<Transform>().SetParent(canvas.GetComponent<Transform>());
     item.GetComponent<Transform>().localPosition = new Vector3(-173f, 162f, 1f);
     item.GetComponent<Transform>().localScale = new Vector3(1f, 1f, 1f);
 
     item.GetComponent<Button>().onClick.AddListener(handleClick);
 
     var item2 = Instantiate(ShopItemPrefab);
     item2.name = "Shop Item 2";
     item2.GetComponent<Transform>().SetParent(canvas.GetComponent<Transform>());
     item2.GetComponent<Transform>().localPosition = new Vector3(175.5f, 162f, 1f);
     item2.GetComponent<Transform>().localScale = new Vector3(1f, 1f, 1f);
 
     item2.GetComponent<Button>().onClick.AddListener(handleClick);
 }

 void handleClick() {
     EventSystem.current.currentSelectedGameObject.GetComponent<ShopItem>().MarkAsSelected();
 }

The MarkAsSelected method is then called on the (supposedly) clicked object and basically marks a boolean as true and changes the background sprite.

Link to GIF

Looking at the Gif above I'm clicking the left box, but the right one is the one that gets triggered. Now the weired thing is that neither objects overlap. However, if in the hierarchy view I move 'Shop Item 2' above 'Shop Item 1' and then click the same box it correctly selects it, but clicking the right box will now trigger the left box.

I hope that makes sense! The confusing thing for me is that I don't understand how this is happening, neither instance is linked together and it's not like this is running inside a loop where their may be problems with the variable scopes.

Thank you in advanced for you help, if you have any questions I'm happily answer them. I just don't know why this is happening!

screen-shot-2017-08-21-at-204923.png (14.8 kB)
screen-shot-2017-08-21-at-204929.png (28.0 kB)
Comment
Add comment · Show 5
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 tablekorner · Aug 22, 2017 at 10:01 AM 0
Share

I've tried to recreate your problem to the best of my ability because from first glance it all appeared to be fine. Using your code you provided without changing anything within it I got the results you desired just as I expected I would with no issue. I believe you error might be co$$anonymous$$g from another segment of code not provided. Perhaps within the ShopItem under $$anonymous$$arkAsSelected()?

In my recreation, my ShopItem prefab was created first with an empty GameObject with only a Button component added after. Then four children, two panels - one foreground, and one background -, an image for the coin, and a text. $$anonymous$$y $$anonymous$$arkAsSelected only changed the color of the background panel to a random color.

avatar image VizuaaLOG tablekorner · Aug 22, 2017 at 11:22 AM 0
Share

Oh thats weird. I'm not at home currently but if I remember correctly the $$anonymous$$arkAsSelected method is the following

 public void $$anonymous$$arkAsSelected() {
     this.selected = true;
     this.background = this.selectedBackground;
 }

this.selected does nothing special it's just a flag so if I needed to I could check if it selected, this.background is a reference to the prefabs background image and this.selectedBackground is a reference to the selected image sprite.

avatar image tablekorner VizuaaLOG · Aug 22, 2017 at 12:11 PM 0
Share

The only thing I can think of is that however you're setting background, it's somehow being set wrong. When you get the chance, find out what happens when you click the right shop item before any hierarchy changes. Then experiment, and see what happens when clicking both boxes after moving 'Shop Item 2' above 'Shop Item 1'.

In my head right now I'm envisioning the way you get this.background might be done in someway using GetComponentInChildren, which would get the first child of whatever type found everytime. Which could explain the functionality change when the hierarchy changes. If this is incorrect, than I'm not sure. As again the code you've provided has worked perfectly in my recreation without any modifications.

Show more comments

2 Replies

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

Answer by VizuaaLOG · Aug 22, 2017 at 07:40 PM

Okay! I just worked it out. I think it was the way I had created my prefab, having the button as the parent and then the background etc as children of it was causing the click to not be detected. I'm not sure whether it was because the image component was not on the button, so it was technically not rendered?

I have recreated the prefab with an empty object that is just the wrapper, and then instead of having a child background object, this is instead a component on the button.

Below is what I have been trying to get for hours!!! Thank you to @Malace and @tablekorner for helping me figure this out :)

alt text


aug-22-2017-20-38-29.gif (61.3 kB)
Comment
Add comment · 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
0

Answer by Malace · Aug 22, 2017 at 04:43 PM

I think the problem is your using the same message for both buttons. So both buttons are firing one event. Which as listeners to that single event. Means your handleClick is run by both buttons no matter which one gets pressed.. Causing whichever one runs last to be your final value of currently selected.

Try 2 events and if your result is as desired. You will need an event that can determine which button triggered the event to set the proper current selection. ie: Just change

 item2.GetComponent<Button>().onClick.AddListener(handleClick);//To
 item2.GetComponent<Button>().onClick.AddListener(handleClick2);// and register a second event for the second button.


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 VizuaaLOG · Aug 22, 2017 at 07:02 PM 0
Share

I get what you're saying but according to the docs EventSystem.current.currentSelectedGameObject should return the last clicked object, so running it through the same handler shouldn't matter.

Also, this is just testing code, once I get this sorted the creation of the buttons will be performed in a loop because there will an unknown amount of items in the shop you can buy.

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

138 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

Related Questions

Create game object from UI button click even 1 Answer

Assigning button On Click() with a prefab 0 Answers

OnClick() event script from a prefab 0 Answers

On Click Missing Audio Object when Scene changes 0 Answers

changing the selected game object in the on click field via script? 0 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