Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 Zeronev · Oct 08, 2015 at 01:15 AM · c#unity 5uibuttononclick

Button only works once

Hello! I've made a function that makes to read a "document", getting the child objects from where the button is, and putting those texts to show. But when I click the document it shows well, but only ONCE!

For example, if I have 2 documents in my inventory, I only can read the first one I click, and when I click another document, it still shows the info about the first one I clicked.

Here's my code that reads documents:

 public void ReadClickedDocument()
     {
 
         Component[] components;
         components = GetComponentsInChildren<Text>();
 
         //The child #0 of the prefab will contain the title of the document, the next one the body.. etc.
         _documentTitle.text = components[1].GetComponent<Text>().text.ToString();
         _documentBody.text = components[2].GetComponent<Text>().text.ToString();
 
         
     }

And this is the code that adds the function to the instantiated button:

 newbuttonToInst.GetComponent<Button>().onClick.AddListener(() => { ReadClickedDocument(); }); //Set the function to read the document
Comment
Add comment · Show 3
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 NDark · Oct 08, 2015 at 03:31 AM 0
Share

I think that's because the scope of ReadClickedDocument(). How about try to add debug log of "the name of game object" in this function to test which object you are actually access?

avatar image Zeronev NDark · Oct 08, 2015 at 11:27 AM 0
Share

Nice, I will try that.

It's getting always the parent's name. So that might be the problem. Any solution?

avatar image Zeronev · Oct 08, 2015 at 11:30 AM 0
Share

Used: Debug.Log(this.gameObject.name);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NDark · Oct 08, 2015 at 02:58 PM

I may try to implement ReadClickedDocument() to a new script binding to button object. And override OnClick() to call this function as well. To achieve your goal.

I didn't use AddListener() before though.

Hope that helps.

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

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

43 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

Related Questions

Add Listeners to array of Buttons 2 Answers

Unity 5: UI button OnPointerDown not function as expected 1 Answer

Unity - keep created buttons after quit 1 Answer

Почему после перехода на другую версию Unity при создании кнопки она не откликается на геймпад и клавиатуру? 0 Answers

How to keep your button selected after clicking away ??? 1 Answer


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