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 Problem with prefabs and lists.

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

  1. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    I have a prefab with a script attached that contains a list and if there is more than one instance of this prefab all of what I think should be individual lists are treated like one list. I'm not sure how to solve this problem.

    Doing a List.Clear() does not work as it clears the data in the scriptableobject I'm getting the pages information from and I have to re-enter the information and it only clears the list of the button I clicked.

    Any help is appreciated, thank you.

    https://i.imgur.com/299Xbi6.jpg - Image of problem

    Code (CSharp):
    1. namespace InGameLibrary
    2. {
    3.     [CreateAssetMenu(fileName = "New Book", menuName = "Scriptable Objects/Book")]
    4.     public class Book_ScriptableObject : ScriptableObject
    5.     {
    6.         public string bookTitle;
    7.         [TextArea] public List<string> pages = new List<string>();
    8.     }
    9. }
    Code (CSharp):
    1. namespace InGameLibrary
    2. {
    3.     [CreateAssetMenu(fileName = "New List of Scriptable Objects", menuName = "Scriptable Objects/ListOf")]
    4.     public class ListOfScriptableObjects : ScriptableObject
    5.     {
    6.         public List<Book_ScriptableObject> listOfScriptableObjects = new List<Book_ScriptableObject>();
    7.     }
    8. }
    Code (CSharp):
    1. namespace InGameLibrary
    2. {
    3.     public class DisplayBookContent : MonoBehaviour
    4.     {
    5.         [SerializeField] private ListOfScriptableObjects lOSO;
    6.         [SerializeField] private List<string> test = new List<string>();
    7.         public Book_ScriptableObject book;
    8.        
    9.         public int pageNumber;
    10.  
    11.         public TMP_Text BookTitleTextBox;
    12.         public TMP_Text BookPageBody;
    13.  
    14.         public GameObject previousPageButton;
    15.         public GameObject nextPageButton;
    16.         public Button thisButton;
    17.         public string thisButtonBookTitle;
    18.  
    19.         private void Start()
    20.         {
    21.             pageNumber = 0;
    22.  
    23.             BookTitleTextBox = GameObject.Find("Book Title").GetComponent<TMP_Text>(); //Text box that holds book.bootTitle
    24.             BookPageBody = GameObject.Find("Page Body").GetComponent<TMP_Text>(); //Text box that holds book.page
    25.             previousPageButton = GameObject.Find("LeftArrowButton");
    26.             nextPageButton = GameObject.Find("RightArrowButton");
    27.             thisButton = transform.GetComponent<Button>();
    28.             thisButtonBookTitle = thisButton.GetComponentInChildren<TMP_Text>().text;
    29.  
    30.             Button pPB = previousPageButton.GetComponent<Button>();
    31.             Button nPB = nextPageButton.GetComponent<Button>();
    32.  
    33.             pPB.onClick.AddListener(PreviousPage);
    34.             nPB.onClick.AddListener(NextPage);
    35.             thisButton.onClick.AddListener(UpdatePage); //When book button is clicked
    36.  
    37.         }
    38.  
    39.         //Called when thisButton is clicked NOT the arrow buttons
    40.         public void UpdatePage()
    41.         {
    42.            
    43.             for (int i = 0; i < lOSO.listOfScriptableObjects.Count; i++)
    44.             {
    45.                 if (lOSO.listOfScriptableObjects[i].bookTitle.Contains(thisButtonBookTitle))
    46.                 {
    47.                     book = lOSO.listOfScriptableObjects[i];
    48.                 }
    49.             }
    50.             BookTitleTextBox.text = book.bookTitle;
    51.             test = book.pages;
    52.             BookPageBody.text = test[0];
    53.         }
    54.  
    55.         private void PreviousPage()
    56.         {
    57.             if (previousPageButton && test != null)
    58.             {
    59.                 if (test.Count > 0)
    60.                 {
    61.                     if (pageNumber > 0)
    62.                     {
    63.                         pageNumber--;
    64.                         BookPageBody.text = test[pageNumber];
    65.                     }
    66.                     else if (pageNumber <= 0)
    67.                     {
    68.                         pageNumber = 0;
    69.                         BookPageBody.text = test[pageNumber];
    70.                     }
    71.                 }
    72.             }
    73.         }
    74.  
    75.         private void NextPage()
    76.         {
    77.             if (nextPageButton && test != null)
    78.             {
    79.                 if (test.Count > 0)
    80.                 {
    81.                     if (pageNumber < test.Count - 1)
    82.                     {
    83.                         pageNumber++;
    84.                         BookPageBody.text = test[pageNumber];
    85.                     }
    86.  
    87.                     else if (pageNumber > test.Count - 1)
    88.                     {
    89.                         pageNumber = test.Count - 1;
    90.  
    91.                         if (pageNumber < 0)
    92.                         {
    93.                             pageNumber = 0;
    94.                             BookPageBody.text = test[pageNumber];
    95.                         }
    96.                         BookPageBody.text = test[pageNumber];
    97.                     }
    98.                 }
    99.             }
    100.         }
    101.     }
    102. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Some variable types are "reference types" and some are "value types". In C#, all primitives (such as int) and structs (such as Vector3) are value types; when you assign one variable to another, value types are copied in their entirety. When you say:
    Code (csharp):
    1. int x = y;
    2. y = 5;
    int is a value type, so when you change y, x is unaffected. This seems to be the mindset you're working with List as.

    Reference types, on assignment, just point to the same object. A reference-type variable points to a spot in memory. All classes (including MonoBehaviour, GameObject, ScriptableObject, and really, most things) are reference types.

    List is a reference type, so when you say:
    Code (csharp):
    1. test = book.pages;
    test is now pointing to the exact same List object as book.pages, which is why they share their contents.

    List has a constructor that makes a copy of the source List, so you'll now have two different List objects you can use separately.
    Code (csharp):
    1. test = new List<string>(book.pages);
     
  3. Crazyspawn

    Crazyspawn

    Joined:
    Jun 17, 2013
    Posts:
    9
    Already tried that and it doesn't work, still have the same problem.