Search Unity

on clicking Need to fetch text value value

Discussion in 'General Discussion' started by ersaurabh101, Mar 23, 2023.

  1. ersaurabh101

    ersaurabh101

    Joined:
    Oct 8, 2010
    Posts:
    412
    **Explanation:**
    I have created a search field. When I type 3 letters, it searches from `Firestore` collection field `tags` and return the result which I instantiate it in a list. Screenshot attached below of the same
    [Search result and list image](https://i.stack.imgur.com/aYBeC.png)
    **The problem:**
    I want to fetch the value of text I have clicked, but I am unable to get it.
    Thanks in advance for you time.
    The name of my script is `search_riddhi.cs`. Please guide me how can I achieve it and what wrong am I doing in my script ?
    ```
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System;
    5. using Riddhi.utility.camera;
    6. public class Search_riddhi : MonoBehaviour
    7. {
    8.     [SerializeField] Transform Content;
    9.     [SerializeField] searchResult_riddhi SearchResult;
    10.     [SerializeField] UnityEngine.UI.InputField field;
    11.     Coroutine k;
    12.     string itemName;
    13.  
    14.     private Camera cam;
    15.     private GameObject pos;
    16.  
    17.     private void Update()
    18.     {
    19.        cam = GameObject.Find("MainCamera").GetComponent<Camera>();
    20.         pos = GameObject.FindWithTag("Tv").GetComponent<GameObject>();
    21.     }
    22.  
    23.  
    24.     public void onEdit()
    25.     {
    26.         for (int i = 0; i < Content.childCount; i++)
    27.         {
    28.             Destroy(Content.GetChild(i).gameObject);
    29.         }
    30.  
    31.         try
    32.         {
    33.             if (MainScreen.instance.objectInfoList.Count < 1) return;
    34.         }
    35.         catch
    36.         {
    37.             return;
    38.         }
    39.        
    40.         if (k != null) StopCoroutine(k);
    41.         if (string.IsNullOrEmpty(field.text))
    42.         {
    43.             k = null;
    44.             return;
    45.         }
    46.         k = StartCoroutine(search());
    47.     }
    48.     IEnumerator search()
    49.     {
    50.         yield return null;
    51.         foreach (var info in MainScreen.instance.objectInfoList)
    52.         {
    53.             if(info.tag.Contains(field.text))
    54.             {
    55.                 Instantiate(SearchResult, Content).setDetails(info.name);
    56.                 SearchResult.GetComponent<Button>().onClick.AddListener(ItemClicked);
    57.          
    58.             }
    59.         }
    60.         k = null;
    61.  
    62.     }
    63.  
    64.  
    65.     void ShowItemDetails()
    66.     {
    67.         // Show the item details for the clicked item here
    68.         Debug.Log("Item Name: " + itemName);
    69.     }
    70.  
    71.  
    72.  
    73.     void ItemClicked()
    74.     {
    75.         Debug.Log("Item clicked");
    76.         Riddhi.utility.camera.Camera_Utility.FocusOn(cam,  pos,  1.5f);
    77.        
    78.     }
    79. }
    80.  
    ```
    after clicking I dont see `Debug.Log("Item clicked");` in console. Please guide