Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice
  2. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Adding OnClick Events to the GameObject during runtime

Discussion in 'AR' started by Daryl2604, Feb 18, 2020.

  1. Daryl2604

    Daryl2604

    Joined:
    Nov 20, 2018
    Posts:
    1
    I am trying to add an OnClick button Listener to a GameObject that is a button in the panel where I want to activate another GameObject to be inserted during runtime in a script.

    This is the code where I have to add the button during runtime and in this one, I want to insert an OnClick Event Listener to this GameObject:-
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Events;
    4. using System;
    5. using System.Collections;
    6. using UnityEditor;
    7. using UnityEditor.Events;
    8.  
    9. public class AddingButtonToPanel: MonoBehaviour
    10. {
    11.     public GameObject buttonPrefab;
    12.     public GameObject panelToAttachButtonsTo;
    13.     public Sprite Image1;
    14.     public Button currentButton;
    15.     public Sprite newSprite;
    16.     public static int counter = 0;
    17.     public Button men;
    18.     GameObject haven;
    19.     Button fourteenBtn;
    20.     Button hippybt;
    21.     public void ButtonAdding()//Creates a button and sets it up
    22.     {
    23.         if (counter < 1)
    24.         {
    25.             GameObject button = (GameObject)Instantiate(buttonPrefab);
    26.             button.SetActive(true);
    27.             // Image1 = GetComponent<Image>();
    28.             Image1 = newSprite;
    29.             button.transform.SetParent(panelToAttachButtonsTo.transform);//Setting button parent
    30.             button.GetComponent<Button>().onClick.AddListener(OnClick);//Setting what button does when clicked
    31.                                                                        //Next line assumes button has child with text as first gameobject like button created from GameObject->UI->Button
    32.             button.transform.GetChild(0).GetComponent<Text>().text = " ";//Changing text
    33.             button.AddComponent<DeletetheObject>();
    34.             Image test = button.GetComponent<Image>();
    35.             test.sprite = newSprite;
    36.             if (button != null)
    37.             {
    38.                 Debug.Log("Button is not null");
    39.                 GameObject ten = GameObject.Find("3D_Object_6");
    40.                // GameObject go = GameObject.FindGameObjectWithTag("obj");
    41.                 //haven = go;
    42.                 //button.GetComponent<Button>().onClick.AddListener(() => go.SetActive(true));
    43.                 if (ten != null)
    44.                 {
    45.                     Debug.Log("this is def not null");
    46.                     // MyScript myScriptInstance = FindObjectOfType<MyScript>();
    47.                     //var targetInfo = UnityEvent.GetValidMethodInfo(myScriptInstance, "OnButtonClick", new Type[] { typeof(GameObject) });
    48.                     //GameObject go = GameObject.FindGameObjectWithTag("obj");
    49.                     //var butt = go;
    50.                     GameObject eleven = GameObject.FindGameObjectWithTag("canvi");
    51.                     if (eleven != null)
    52.                     {
    53.                         Debug.Log("There are 12 angry men");
    54.                         GameObject twelve = eleven.transform.GetChild(4).gameObject;
    55.                         if (twelve != null)
    56.                         {
    57.                             Debug.Log("There are 12 angry men and it is too much");
    58.                             GameObject thirteen = twelve.transform.GetChild(1).gameObject;
    59.                             if (thirteen != null)
    60.                             {
    61.                                 Debug.Log("There are 13 angry WOMEN WOULD BE NICE");
    62.                                 //GameObject fourtheen = GameObject.FindGameObjectWithTag("test");
    63.                                 GameObject fourtheen = thirteen.transform.GetChild(5).gameObject;
    64.                                 GameObject fourtheen1 = thirteen.transform.GetChild(5).gameObject;
    65.                                 GameObject fourtheen2 = thirteen.transform.GetChild(5).gameObject;
    66.                                 GameObject fourtheen3 = thirteen.transform.GetChild(5).gameObject;
    67.                                 GameObject fourtheen4 = thirteen.transform.GetChild(5).gameObject;
    68.                                 if (fourtheen != null)
    69.                                 {
    70.                                     Debug.Log("putting it there");
    71.                                     //fourtheen.GetComponent<Button>().onClick.AddListener(delegate { objobj(); });
    72.                                     fourteenBtn = fourtheen.GetComponent<Button>();
    73.                                     if(fourteenBtn != null)
    74.                                     {
    75.                                         GameObject hip = GameObject.FindGameObjectWithTag("obj");
    76.                                         fourteenBtn.GetComponent<Button>().onClick.AddListener(() => hip.SetActive(true));
    77.                                         //UnityAction<GameObject> callback = new UnityAction<GameObject>(objobj);
    78.                                         //UnityEventTools.AddObjectPersistentListener<GameObject>(fourteenBtn.onClick, callback, eleven);
    79.                                         //UnityEditor.Events.UnityEventTools.AddPersistentListener(fourteenBtn.onClick, new UnityAction(() => go.SetActive(false)));
    80.                                         //Debug.Log("haha");
    81.                                         //Debug.Log("haha_me");
    82.                                         //Debug.Log(fourteenBtn.name);
    83.  
    84.                                     }
    85.                                
    86.                                 }
    87.                              
    88.                             }
    89.                         }
    90.                     }
    91.                  
    92.                     //UnityAction<GameObject> action = new UnityAction<GameObject>(go.SetActive(true));
    93.                     // UnityEventTools.AddObjectPersistentListener<GameObject>(butt.SetActive(true), action);
    94.                     if (eleven != null)
    95.                     {
    96.  
    97.                         Debug.Log("Eleven is not null");
    98.                     }
    99.                     else
    100.                     {
    101.                         Debug.Log("Eleven is null");
    102.                     }
    103.                     // eleven.GetComponent<Button>().onClick.AddListener(() => objobj());
    104.                 }
    105.                 else {
    106.                     Debug.Log("def null");
    107.                 }
    108.             }
    109.  
    110.             if (test == null)
    111.             {
    112.                 Debug.Log("Its null");
    113.             }
    114.             else
    115.             {
    116.                 Debug.Log("Its not null");
    117.             }
    118.  
    119.             counter++;
    120.         }
    121.  
    122.         else
    123.         {
    124.  
    125.         }
    126.     }
    127.     void OnClick()
    128.     {
    129.         Debug.Log("clicked!");
    130.     }
    131.  
    132.     public void AttachToPanel()
    133.     {
    134.         ButtonAdding();
    135.     }
    136.  
    137.     void MyAction()
    138.     {
    139.      
    140.     }
    141.  
    142.    public void objobj(GameObject eleven)
    143.    {
    144.        // GameObject hip = GameObject.FindGameObjectWithTag("gen");
    145.        // if (hip != null)
    146.        // {
    147.           //  GameObject hippy = hip.transform.GetChild(5).gameObject;
    148.            // if (hippy != null)
    149.            // {
    150.              //   hippy = GameObject.Find("Object_5(Clone)");
    151.                 //eleven = hippy.SetActive(true);
    152.              //   if (hippybt != null)
    153.              //   {
    154.                     //hippybt = GameObject.Find("Object_5(Clone)");
    155.                     //UnityAction<GameObject> callback = new UnityAction<GameObject>(objobj);
    156.                     //UnityEventTools.AddObjectPersistentListener<GameObject>(hippybt.onClick, callback, eleven);
    157.               //  }
    158.           //  }
    159.  
    160.       }
    161.      
    162.  
    163. }
    164.  
    165. //public class MyScript : MonoBehaviour
    166. //{
    167.    // public void OnButtonOnClick(GameObject sender)
    168.    // {
    169.  
    170. //}
    171. //}
    This is the object that I have to add:-
    1.1) Generator.JPG

    Into this GameObject in the OnClick of the next image :-
    1.2) Canvas.3DObject.JPG

    The OnClick Events which is empty for 3D_Object_6(Clone)
    1.3) OnClick().now.JPG

    Should come out like this during runtime:-
    1.4) OnClick().whatitshouldbe.JPG


    This is another GameObject which is a button that I need to click to instantiate 1.1 and 1.2 to the scene:-
    1.5) Button.ThatINeedToClick.JPG

    I just need this to be for an Augmented Reality App where I have very little experience of anything like this and I need a helping hand.
     
  2. d4n3x

    d4n3x

    Joined:
    Jan 23, 2019
    Posts:
    24
    I guess u tried to make it clear. But still, I don't really get the point why a AddListener do not work.

    And a small Addition to picture 1.4) - I guess this is just a prototype but you should definetly not add that much functions in an onClick method. Especially when there are 5 calls which all just deactivate a panel.

    Greets Danex
     
  3. trakevital

    trakevital

    Joined:
    Sep 25, 2020
    Posts:
    2
    I have a similar problem
     
  4. jack45645

    jack45645

    Joined:
    Sep 16, 2020
    Posts:
    7
    have you find way to use onclick method on gameobject
     
  5. pharamodel4567

    pharamodel4567

    Joined:
    Jan 17, 2021
    Posts:
    3
    if i have 100 btn and i need one function onclick for to 100 btn what is the solution for that ,,
     
unityunity