Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question My button responds to transition click but not to Onclick events

Discussion in 'UGUI & TextMesh Pro' started by cloa513, Jan 21, 2022.

  1. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    upload_2022-1-22_8-31-55.png


    Code (CSharp):
    1.  private float pxn;
    2.         private float pyn;
    3.         public Values value1;
    4.         [SerializeField] private GameObject B2;
    5.        
    6.         private void FixedUpdate()
    7.         {
    8.          
    9.         }
    10.  
    11.         private void Start()
    12.         {
    13.              px = transform.position.x;
    14.                         py = transform.position.y;
    15.                         pxn=value1.xst+(int)((px - (value1.xst))/(value1.xs*3));
    16.                         pyn=value1.yst+(int)((py - (value1.yst))/(value1.ys*3));
    17.         }
    18.  
    19.  
    20.  
    21.         void createmain()
    22.         {
    23.             print("click");
    24.            Instantiate(B2, new Vector3(pxn, pyn), Quaternion.identity);
    25.            
    26.  
    27.         }
    28.        
    29.     }
    30.  
     
  2. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
    Here is the creation code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CreateButtons : MonoBehaviour
    7. {  [SerializeField] private GameObject ts;
    8.     private GameObject _a;
    9.     public Canvas canvas1;
    10.     public Values value1;
    11.     private float _px;
    12.    
    13.     private float _py;
    14.  
    15.     private string _rowcol;
    16. //    private int _tempnum;
    17.     private int _num;
    18.  
    19.     private int _row;
    20.  
    21.     private int _col;
    22.  
    23.     private int _squ;
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {var _py1=value1.yst;
    27.      var _px1=value1.xst;
    28.      var _px2=value1.xs;
    29.      var _py2=value1.ys;
    30.  
    31.         for (var i = 0; i < (value1.col*3); i++) //value1.row is number of rows starting value 9
    32.         {
    33.            
    34.             for (var j = 0; j < (value1.row*3); j++) //value1.row is number of columns starting value 9
    35.             {_py = _py1 + (_py2 * i);
    36.                 _px = _px1 + (_px2 * j);
    37.              
    38.                
    39.                
    40.                             _a=Instantiate(ts,new Vector3(_px,_py),Quaternion.identity);
    41.                            _a.transform.SetParent (canvas1.transform,false);
    42.                  
    43.                            
    44.             }
    45.          
    46.             void TaskOnClick()
    47.             {
    48.                 //Output this to console when Button1 or Button3 is clicked
    49.                 Debug.Log("You have clicked the button!");
    50.             }                
    51.  
    52.         }
    53.     }
    54.   [ATTACH=full]991833[/ATTACH]
    55.    
    56.  
    57.  
    58.  
    59.     // Update is called once per frame
    60.     void Update()
    61.     {
    62.        
    63.     }
    64. }
    65.  
    They respond to mouse click for a transition. Please tell me how to fix this. I tried onclick.listener but Unity doesn't like that because buttons in Unity 2021 don't need that.
     

    Attached Files:

    Last edited: Jan 22, 2022
  3. cloa513

    cloa513

    Joined:
    Jan 3, 2016
    Posts:
    79
  4. Dredika

    Dredika

    Joined:
    May 16, 2020
    Posts:
    4
    I am having the same issue, though my button was created manually in the editor.

    An EventSystem is present; the transition effects wouldn't fire otherwise.
    Tried hooking up the event from the editor and also adding a listener in code.

    It makes no sense that the transitions work - they are triggered from the same Pointer events as the OnClick...

    EDIT
    On further investigation, it seems the problem (at least for me) lies in the Input Action mappings in the New Input System. The UI Click actions must be set to Action Type 'Pass Through' for the button OnClick event to fire.

    My Click actions were set to Button action type, kind of weird that this doesn't trigger actual Button components...
     
    Last edited: Jul 6, 2022