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 Change button text to string from list.

Discussion in 'Scripting' started by Crazyspawn, Jul 25, 2021.

  1. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    I'm trying to create a library in my game where the player can search with a keyword and a list of books with that keyword appear with a clickable button that opens to that books specific pages.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BookCollection : MonoBehaviour
    6. {
    7.     public List<Book> books = new List<Book>();
    8.     public void Start()
    9.     {
    10.         books.Add(new Book("Book of Fire", "fire, heat, burn, scorch, light"));
    11.         books.Add(new Book("Book of Water", "water, wet, ocean, lake, river, stream, pond"));
    12.         books.Add(new Book("Book of Watery Water", "water, wet, ocean, lake, river, stream, pond"));
    13.         books.Add(new Book("Book of Earth", "earth, dirt, soil, rocks, stones"));
    14.         books.Add(new Book("Book of Air", "air, wind, breeze, cool"));
    15.     }
    16. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class SearchForBook : MonoBehaviour
    8. {
    9.     private TMP_InputField inputField;
    10.     private TMP_InputField.SubmitEvent submitEvent;
    11.     private BookCollection bookList;
    12.     private List<string> matchedBooks = new List<string>();
    13.  
    14.     public GameObject contentBox; //Scroll view content box
    15.     public GameObject prefab;
    16.  
    17.     private void Start()
    18.     {
    19.         bookList = gameObject.GetComponent<BookCollection>();
    20.         inputField = gameObject.GetComponent<TMP_InputField>();
    21.         submitEvent = new TMP_InputField.SubmitEvent();
    22.         inputField.onEndEdit.AddListener(SearchBooks);
    23.     }
    24.  
    25.     private void SearchBooks(string keyword)
    26.     {
    27.         matchedBooks.Clear();
    28.         for (int i = 0; i < bookList.books.Count; i++)
    29.         {
    30.             if (bookList.books[i].tags.Contains(keyword))
    31.             {
    32.                 matchedBooks.Add(bookList.books[i].bookTitle);
    33.             }
    34.         }
    35.         PopulateKeywordMatchBookList();
    36.     }
    37.  
    38.     private void PopulateKeywordMatchBookList()
    39.     {
    40.         //Clears out prefabs before adding new ones
    41.         foreach (Transform child in contentBox.transform)
    42.         {
    43.             Destroy(child.gameObject);
    44.         }
    45.  
    46.         for (int i = 0; i < matchedBooks.Count; i++)
    47.         {
    48.             Instantiate(prefab, contentBox.transform);
    49.         }
    50.     }
    51.  
    52. }
    I'm at the button part and I'm not sure how to change the button text to the book title from the list.
    I'm still a beginner so I hope all this makes sense, any and all help is appreciated. Thank you.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    If you have buttons, they come with a gameObject as a child with a Text component on them. Or a TextMeshProUGUI component on it, depending on which you go with.

    You just loop through your buttons, grab the Component in the child, and change the text on it.

    I'm actually not sure what part is confusing? Create a list of buttons, or a list of their text components, loop through it, populating each from your list of books.
     
  3. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    Figured out how to do it this way and it works.

    Code (CSharp):
    1. for (int i = 0; i < matchedBooks.Count; i++)
    2.         {
    3.             Instantiate(prefab, contentBox.transform).GetComponentInChildren<TMP_Text>().text = matchedBooks[i];
    4.         }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    This looks great with one comment: it is hairy code and you should consider breaking it into separate lines, in case in the future you change (For instance) the TMP_Text to be something else, and then mysteriously start getting null references. The way it is above you won't have any idea what is actually null.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.