Search Unity

Instantiated Button Prefab Addlistener is triggered clicking ANYwhere...

Discussion in 'UGUI & TextMesh Pro' started by Discmage, Apr 8, 2019.

  1. Discmage

    Discmage

    Joined:
    Aug 11, 2014
    Posts:
    60
    I have been looking for a solution for this but con't seem to find a specific solution so here goes...theres a lot of 'similar' situations...but I can't seem to fix this one.

    I have created a button prefab that I instantiate in the world and set the parent to where it needs to be (I am using a scrollrect for example)...the position and everything works fine apart from the fact that I can click ANYWHERE on the screen to trigger the addlistener event rather than just on the button.

    All the canvas objects and children have a scale of 1,1,1. When I debug which button is being clicked by name it comes up as the correct name, even though I'm not clicking ON the button.

    I attempted moving all the parents of the button to be smaller to see if the hitzone was on those, but that didn't seem to make any difference.

    I use invoke to set the button up in Start() as I need to load text first and place the button after the textfield.

    Any help would be appreciated!

    Invoke("LoadButton", 1);

    Code (CSharp):
    1.    
    2.  
    3. void LoadButton() {
    4.     RollButton("test", "Investigation", 1, 1);
    5.     }
    6.  
    7. public void RollButton(string buttonName, string rollType, int currentPage, int currentDecision) {
    8.  
    9.     // Get textfield parent scale information
    10.     float scale = worldObjects.TXT_Page_0.rectTransform.localScale.y * worldObjects.TXT_Page_0.rectTransform.sizeDelta.y;
    11.     worldObjects.canvasParent.GetComponent<RectTransform>().sizeDelta = new Vector2(8, scale + 3);
    12.  
    13.     // Get textfield positioning information for button position
    14.     Vector3[] worldCorners = new Vector3[4];
    15.     worldObjects.TXT_Page_0.rectTransform.GetWorldCorners(worldCorners);
    16.     float bottom1 = worldCorners[0].y;
    17.  
    18.     // Spawn New Button at bottom of text field
    19.     newButton = Instantiate(worldObjects.buttonTest, new Vector3(0, bottom1, 0), Quaternion.identity);
    20.     newButton.name = "test";
    21.     newButton.GetComponentInChildren<TextMeshProUGUI>().text = "Roll for " + rollType;
    22.     newButton.transform.SetParent(worldObjects.canvasParent.transform, true);
    23.  
    24.     // If button is clicked
    25.     newButton.GetComponent<Button>().onClick.AddListener(() => OnButtonClick(newButton, buttonName, rollType, currentPage, currentDecision));
    26.  
    27.     float bottom2 = worldCorners[0].y;
    28.     }
    29.  
    30. public void OnButtonClick(Button newButton, string buttonName, string rollType, int currentPage, int currentDecision) {
    31.  
    32.     Debug.Log(EventSystem.current.currentSelectedGameObject.name);
    33.  
    34.     Destroy(newButton.gameObject);
    35.  
    36.     int currentRoll = Random.Range(1, 21);
    37.     Debug.Log(currentRoll);
    38.  
    39.     if (newButton.name == "test") {
    40.         if (currentRoll >= 10) {
    41.         worldObjects.TXT_Page_0.text = worldObjects.TXT_Page_0.text + "<br><br> He looks around the room to find everything!";
    42.         } else {
    43.         worldObjects.TXT_Page_0.text = worldObjects.TXT_Page_0.text + "<br><br> He looks around the room to find nothing!";
    44.         }
    45.     }
    46.     }