Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate prefab with multiple texts

Discussion in 'Scripting' started by glgamesforfun, Jan 15, 2022.

  1. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    Hello,
    I want to save Usernames and a Highscore attached to it. I have 2 texts on my prefab and i want to ask, how i can instantiate both an give them different values?
    Thanks!
    Code (CSharp):
    1.     public InputField nameInput;
    2.     public GameObject usernamePrefab;
    3.     public GameObject container;
    4.     [SerializeField] private int itemsToGenerate;
    5.  
    6.     public List<string> playerNames = new();
    7.     public List<int> playerValues = new();
    8.  
    9.     void Start()
    10.     {
    11.         //PlayerPrefs.DeleteAll();
    12.         itemsToGenerate = PlayerPrefs.GetInt("USERNAME_COUNT");
    13.  
    14.         for (int i = 0; i < itemsToGenerate; i++)
    15.         {
    16.             string player = PlayerPrefs.GetString("USERNAME" + i);
    17.             int value = PlayerPrefs.GetInt("HIGHSCORE" + i);
    18.             playerNames.Add(player);
    19.             playerValues.Add(value);
    20.             InstantiateUsername(player, value);
    21.         }
    22.     }
    23.  
    24.     public void SaveList()
    25.     {
    26.         for (int i = 0; i < playerNames.Count; i++)
    27.         {
    28.             PlayerPrefs.SetString("USERNAME" + i, playerNames[i]);
    29.             PlayerPrefs.SetInt("HIGHSCORE" + i, playerValues[i]);
    30.         }
    31.         PlayerPrefs.SetInt("USERNAME_COUNT", playerNames.Count);
    32.     }
    33.  
    34.     public void AddUsernames()
    35.     {
    36.         if (nameInput.text != null)
    37.         {
    38.             if (!playerNames.Contains(nameInput.text))
    39.             {
    40.                 playerNames.Add(nameInput.text);
    41.                 playerValues.Add(0);
    42.                 InstantiateUsername(nameInput.text, 0);
    43.             }
    44.         }
    45.         SaveList();
    46.     }
    47.  
    48.     void InstantiateUsername(string text, int text2)
    49.     {
    50.         var username = Instantiate(usernamePrefab);
    51.         username.transform.SetParent(container.transform, false);
    52.         username.transform.localScale = Vector3.one;
    53.         username.GetComponentInChildren<Text>().text = text;
    54.         username.GetComponentInChildren<Text>().text = text2.ToString();
    55.     }
    56. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    For regular grid or list of UI items things, this is my go-to pattern: I make a single example and then clone it as maby times as I need. See example package.

    NOTE: there is no prefab: the "example" is lying in the scene and I turn it inactive, then make clones off of it, turning each clone active. Personally I find prefabs for UI elements to be awkward and prone to all kinds of anchoring and scaling errors, so I like to keep them right in the scene as the UI gets developed.
     

    Attached Files:

    Last edited: Jan 15, 2022
    glgamesforfun likes this.