Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 cow_co · Mar 09, 2015 at 04:25 AM · uiinstantiatebutton

An Instantiated UI Button doesn't work; clicking does nothing.

I'm trying to make a system where the player can attach different "modules" to "nodes on their vehicle. So I want the player to be able to enter the inventory, showing all their modules, click a module they want to attach, at which point their ship is displayed, with images highlighting each available attachment node. These images will be buttons which, once clicked, attach the chosen module to that attachment point. I have a script which instantiates the buttons as children of some empty "attach point" game objects which are in turn children of the player's vehicle. So the buttons show up fine, but I can't interact with them. I click them but nothing happens. I've checked that there's an EventSystem in the hierarchy and that the buttons are set to interactable.

Here are some pictures of the component setup on the button prefab:

alt text

alt text

The code which instantiates the buttons (which is working) is

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using Assets._Custom._Scripts._Helpers;
 using Assets._Custom._Scripts._ShipScripts;
 
 namespace Assets._Custom._Scripts._GUIScripts
 {
     public class ShipImageDisplay : MonoBehaviour 
     {
         private GameObject shipObject;
         private ShipData playerShipInfo;
         public GameObject shipDisplayPoint;
 
         public GameObject nodeImagePrefab;    //the prefab that will be instantiated at each available attachment node.
         public Module chosenModule;
 
         private void OnEnable()
         {
             shipObject = GameObject.Find("PlayerShip001");
             playerShipInfo = shipObject.GetComponent<Ship>().data;
             int numberOfAvailableNodes = playerShipInfo.shipAvailableAttachPoints.GetLength(0);
             Debug.Log (numberOfAvailableNodes);
 
             for (int i = 0; i < numberOfAvailableNodes; i++)
             {
                 GameObject nodeDisplay = Instantiate(nodeImagePrefab) as GameObject;
                 nodeDisplay.transform.position = playerShipInfo.shipAvailableAttachPoints[i].position;
                 nodeDisplay.transform.SetParent(playerShipInfo.shipAvailableAttachPoints[i], false);
                 nodeDisplay.GetComponentInChildren<NodeBtnScript>().nodeIndex = i;
                 nodeDisplay.GetComponentInChildren<NodeBtnScript>().module = chosenModule;
             }
 
             Messenger.Broadcast(Broadcasters.ModuleChosenEvent.ToString());
         }
     }
 }

The code on the NodeBtn script is

 using System;
 using System.Collections;
 using Assets._Custom._Scripts._ShipScripts;
 using Assets._Custom._Scripts._Helpers;
 
 namespace Assets._Custom._Scripts._GUIScripts
 {
     public class NodeBtnScript : MonoBehaviour 
     {
         public int nodeIndex;    //the index  (in the available nodes array) of the node this image is highlighting.
         public Module module;    //the module we're attaching.
 
         private Button thisBtn;
 
         private void Awake()
         {
             thisBtn = GetComponent<Button>();
             thisBtn.onClick.AddListener(Click);
         }
 
         public void Click()
         {
             /*For some reason, Messenger broadcasting was messing up, so I'll try to fix that later on.*/
             Messenger<Module, int>.Broadcast(Broadcasters.ModuleAttachEvent.ToString(), module, nodeIndex);
 
             Debug.Log ("Node chosen");
         }
     }
 }


Any advice and help you could provide would be much appreciated.

screenshot-50.png (6.9 kB)
screenshot-49.png (41.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
1

Answer by AlwaysSunny · Mar 09, 2015 at 04:25 AM

When you instantiate a button, it may lose the reference upon which any assigned OnClick events depend. (If that reference is part of the prefab which contains the button, I suppose it would remain intact.)

My favorite way around this involves some custom call-backs. My runtime-instantiated buttons have a script which invokes my logic on their mouse-related events and calls a parent managerial script to act upon those events.

Here are the interfaces you can support for buttons:

     IPointerEnterHandler,
     IPointerExitHandler,
     IPointerDownHandler,
     IPointerUpHandler,
     IPointerClickHandler,
     IDragHandler,
     IDropHandler,
     IScrollHandler,
     IUpdateSelectedHandler,
     ISelectHandler,
     IDeselectHandler,
     IMoveHandler

And a samlpe from my button script:

 public class UIButtonActual : MonoBehaviour, IPointerEnterHandler    
     public void OnPointerEnter( PointerEventData ped ) {
       // do whatever here, e.g. call a managerial class and pass "this" to know which button was pressed.
       // of course there are other ways to handle this situation, but I like this approach myself.
     }

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 cow_co · Mar 09, 2015 at 06:40 AM 0
Share

Ok, I see. Thank you very much, I'll try that tonight.

avatar image cow_co · Mar 09, 2015 at 04:44 PM 0
Share

So could I make it inherit from IPointerClickHandler, and then use OnPointerClick to execute whatever I need to do when the player clicks the button?

avatar image AlwaysSunny · Mar 09, 2015 at 10:50 PM 0
Share

It's not "inheritance," but "implementation". You inherit from a class, but you implement an interface.

And yes, that's exactly correct! There are other ways to achieve this, but I believe knowing this interface approach is very useful.

avatar image cow_co · Mar 10, 2015 at 02:08 AM 0
Share

Alright, thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how can i clone ui button ? 2 Answers

Is there a way to Instantiate a button prefab as a child of a Canvas? 2 Answers

Pressing space calls the wrong function 1 Answer

Changing UI Buttons color 3 Answers

Unity UI Button 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