• 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 moepmoep12 · Aug 09, 2018 at 11:51 AM · programmingreferencingpattern

Reference to components of UI widget

Hey,

after some time developing with Unreal I'm starting to explore Unity. I'm looking for advice for the following task.

In my project the user can click and select objects with the mouse. The object is then highlighted. I'd like to add a widget that displays some information about the object and a context menu with buttons for interaction. The information about the object as well as the callbacks for the different OnClick events are added during runtime.

I've got the widget setup as follows:

 WidgetCanvas
   -TextPanel
      - InfoText
   -ContextMenuPanel
      -ActionButton
         -ActionButtonText (dynamic)
      -DeleteButton
         -DeleteButtonText (static)

So when an object gets selected it should create the widget and provide the information to display and register its callbacks. Here's my question, how do I get a reference to the various UI elements without searching through all child component by name?

My first naive approach was to create an empty gameObject with an attached script and the UI widget as a child. The script holds the references for the UI Elements and provides public methods for registering callbacks, like

 void Start() {
     GetComponentsInChildren<Button>(buttons);
     ActionButton = buttons.Find(b => b.name.Equals("ActionButton"));
 }

 public void RegisterOnActionButtonClicked(UnityAction action)
     {
         ActionButton.onClick.AddListener(action);
     }

This approach feels "dirty" and hard coded. Every time I change the widget or button names I'd have to change my code as well. Is there some common pattern to use?

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

160 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

Related Questions

How to automatically change the prefabs of breakout bricks on hit? 0 Answers

Trying to reference a float, but it wont work? 1 Answer

Does scriptable object data persist through List<> or Array changes? 0 Answers

Access variables or methods from another class (or gameObject) [C#] 1 Answer

Having referencing Problems while creating a tile brush for an editor in unity 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges