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 Need help with list and scriptable objects.

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

  1. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    There's probably an obvious answer here but I'm too tired to figure it out at the moment.

    How I got it to work is in another script I search with a keyword which looks through a list of books. I then take the matches from that and search through a scriptableobject that has a list of scriptableobjects until finally I take the matches from there and put them on individual buttons. Clicking on a button brings up the first page from the list in the scriptableobject book.

    So right now I am trying to access the "pages" part of a scriptableobject which I can do, the problem occurs when I have multiple buttons. What happens is when I click one button the pages of that are stored in the test list but are not dropped when I click another button. For example, if I have 3 pages from one button and then the next button I click has 2 now the list has 5 pages.

    Clearing the list doesn't work as it just resets the scriptableobject and I have to put all the info back in again. Through testing I figured out that if I search with a keyword again it fixes the problem but I'm wondering if that's the only way or if I'm just being dumb.

    Thanks in advance.


    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 FindBookPages : MonoBehaviour
    8. {
    9.     [SerializeField] private ListofPages listofPages;
    10.  
    11.     public Pages bookPages;
    12.     public string bookTitle;
    13.  
    14.     [SerializeField] private TMP_Text pageBookTitle;
    15.     [SerializeField] private TMP_Text currentPage;
    16.     private List<string> test = new List<string>();
    17.     private GameObject previousPageButton;
    18.     private GameObject nextPageButton;
    19.  
    20.     [SerializeField] private int pageNumber;
    21.  
    22.     private void Start()
    23.     {
    24.         Setup();
    25.  
    26.         previousPageButton = GameObject.Find("LeftArrowButton");
    27.         nextPageButton = GameObject.Find("RightArrowButton");
    28.         Button pPB = previousPageButton.GetComponent<Button>();
    29.         Button nPB = nextPageButton.GetComponent<Button>();
    30.  
    31.         pPB.onClick.AddListener(PreviousPage);
    32.         nPB.onClick.AddListener(NextPage);
    33.  
    34.     }
    35.  
    36.  
    37.  
    38.     public void Setup()
    39.     {
    40.         bookTitle = transform.GetComponentInChildren<TMP_Text>().text;
    41.         for (int i = 0; i < listofPages.pageScriptableObjects.Count; i++)
    42.         {
    43.             if (listofPages.pageScriptableObjects[i].name.Contains(bookTitle))
    44.             {
    45.                 bookPages = listofPages.pageScriptableObjects[i];
    46.             }
    47.         }
    48.     }
    49.  
    50.     //Called on prefab button OnClickEvent
    51.     public void UpdatePage()
    52.     {
    53.         pageBookTitle = GameObject.Find("Book Title").GetComponent<TMP_Text>();
    54.         currentPage = GameObject.Find("Page Body").GetComponent<TMP_Text>();
    55.         pageBookTitle.text = bookTitle;
    56.         test = bookPages.pages;
    57.         if(test.Count > 0)
    58.         {
    59.             currentPage.text = test[0];
    60.         }
    61.         pageNumber = 0;    
    62.     }
    63.  
    64.     private void PreviousPage()
    65.     {
    66.         if(previousPageButton != null)
    67.         {
    68.             if(test.Count > 0)
    69.             {
    70.                 if (pageNumber > 0)
    71.                 {
    72.                     pageNumber--;
    73.                     currentPage.text = test[pageNumber];
    74.                 }
    75.                 else if (pageNumber <= 0)
    76.                 {
    77.                     pageNumber = 0;
    78.                     currentPage.text = test[pageNumber];
    79.                 }
    80.             }
    81.         }
    82.     }
    83.  
    84.     private void NextPage()
    85.     {
    86.         if (nextPageButton != null)
    87.         {
    88.             if(test.Count > 0)
    89.             {
    90.                 if (pageNumber < test.Count - 1)
    91.                 {
    92.                     pageNumber++;
    93.                     currentPage.text = test[pageNumber];
    94.                 }
    95.              
    96.                 else if (pageNumber > test.Count - 1)
    97.                 {
    98.                     pageNumber = test.Count - 1;
    99.              
    100.                     if (pageNumber < 0)
    101.                     {
    102.                         pageNumber = 0;
    103.                         currentPage.text = test[pageNumber];
    104.                     }
    105.                     currentPage.text = test[pageNumber];
    106.  
    107.                 }
    108.             }
    109.         }
    110.     }
    111. }
     
    Last edited: Jul 27, 2021
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Sounds like a reference type problem.
    When you do
    List<int> a = b;
    Well, now you have 2 copies of the list, but in the sense, that they are the exact same list. Modifying a will modify b.
    Instead you want to deep copy the list.
    List<int> a = new List<int>(b);
     
  3. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    Still having the same problem, where in my script would I do that?

    Also figured out that the only reason why searching with a keyword fixes the problem is that it destroys the buttons and then makes new ones, so I think that's a dead end.
     
    Last edited: Jul 27, 2021
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Well, I don't see the code (at least I don't think I do) where you try to replace the pages.