Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thomasdamri · Oct 27, 2018 at 08:28 PM · 2dlistrandomchoose

Choose random prefabs from list?

Hi, so i made a list with 10 prefabs in, and i want the shop to randomly pick 5 from the list everytime the shop is opened. I am really new to unity and have been following tutorials up to this point. Thanks. alt text

The code is

 namespace Assets.Scripts.UserInterface
 {
     public class StoreUIController : MonoBehaviour
     {
       
         public ItemList MerchantInventory;
 
         [SerializeField]
         private GameObject ItemTemplate;
         [SerializeField]
         private GameObject CurrencyTemplate;
         
         private Transform _scrollViewContent;
 
         private PlayerInventoryUIController _playerInventoryUI;
 
         private void Start()
         {
             _playerInventoryUI = FindObjectOfType<PlayerInventoryUIController>();
         }
 
         public void PopulateInventory(ItemList inventoryList)
         {
             ClearInventory();
             MerchantInventory = inventoryList;
 
             if (_scrollViewContent == null)
             {
                 _scrollViewContent = transform.Find("Scroll View").Find("Viewport").Find("Content"); ;
             }
 
             foreach (var item in inventoryList.ClothingItems)
             {
                 GameObject newItem = Instantiate(ItemTemplate, _scrollViewContent);
 
                 newItem.transform.localScale = Vector3.one;
                 newItem.transform.Find("Image").GetComponent<Image>().sprite = item.Sprite;
                 newItem.transform.Find("Name").GetComponent<Text>().text = item.Name;
                 newItem.transform.Find("Description").GetComponent<Text>().text = item.Description;
 
                 foreach (var cur in item.PurchasePrice)
                 {
                     GameObject newCurrency = Instantiate(CurrencyTemplate, newItem.transform.Find("Currency/List"));
 
                     newCurrency.transform.localScale = Vector3.one;
                     newCurrency.transform.Find("Image").GetComponent<Image>().sprite = cur.Currency.Image;
                     newCurrency.transform.Find("Amount").GetComponent<Text>().text = cur.Amount.ToString();
 
                 }
                 newItem.transform.Find("Currency").Find("BuyBtn").GetComponent<Button>().onClick.AddListener(BuyOnClick);
 
             }
             
 
         }
         public void ClearInventory()
         {
             MerchantInventory = null;
 
             //Since this starts out as disabled, we need to do a check the first time we try to access the content element, as we may not have a reference to it.
             if (_scrollViewContent == null)
             {
                 _scrollViewContent = transform.Find("Scroll View").Find("Viewport").Find("Content");
             }
 
             foreach (Transform child in _scrollViewContent)
             {
                 Destroy(child.gameObject);
             }
         }
 
         public void BuyOnClick()
         {
             Item purchasedItem = MerchantInventory.ClothingItems.Find(x => x.Name.Equals(EventSystem.current.currentSelectedGameObject.transform.parent.parent.Find("Name").GetComponent<Text>().text));
 
             if (purchasedItem == null)
             {
                 Debug.Log("Unable To Find Item In Database");
                 return;
             }
             else if (purchasedItem.PurchasePriceInPound() >= _playerInventoryUI.PlayerInventory.PoundCoins)
             {
                 Debug.Log("Cannot Afford Item");
                 return;
             }
 
             _playerInventoryUI.PurchaseItem(purchasedItem);
 
             MerchantInventory.ClothingItems.Remove(purchasedItem);
 
             PopulateInventory(MerchantInventory);
         }
         
     }
 }
forum.png (16.5 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Oct 27, 2018 at 08:49 PM 0
Share

that's the code alright, but what is your problem?

avatar image thomasdamri hexagonius · Oct 27, 2018 at 08:55 PM 0
Share

Sorry, at the moment the code here populates the shop with everything in the list. I was wondering what i would have to do to make the shop get populated by only 5 items which are randomly selected from the list each time.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by UnityCoach · Oct 27, 2018 at 09:06 PM

Hi,

that's a good question. Randomly pick one item from a list is easy. You can simply do something like :

 myListOfItems[Random.Range(0, myListOfItems.Length)] // if it's an array
 myListOfItems[Random.Range(0, myListOfItems.Count)] // if it's a List

But picking a given number of items from a list is a bit more interesting.

Let me give this a try and explain through it. Let's begin with the method signature (our goal).

 public static List<T> GetRandomItemsFromList<T> (List<T> list, int number)

This will be a generic method, with type T so that it can work with a list of anything. It will take a list, and a number of items to pick for parameters. Then, we want to duplicate the input list, so that we can remove items from it as we add them to a new list. Eventually, we want to return the new list.

 // this is the list we're going to remove picked items from
 List<T> tmpList = new List<T>(list);
 // this is the list we're going to move items to
 List<T> newList = new List<T>();
 return newList;

Now we want to loop and move items from one list to the other.

 // make sure tmpList isn't already empty
 while (newList.Count < number && tmpList.Count > 0)
 {
     int index = Random.Range(0, tmpList.Count);
     newList.Add(tmpList[index]);
     tmpList.RemoveAt(index);
 }

So, here's the whole method :

 public static List<T> GetRandomItemsFromList<T> (List<T> list, int number)
 {
     // this is the list we're going to remove picked items from
     List<T> tmpList = new List<T>(list);
     // this is the list we're going to move items to
     List<T> newList = new List<T>();
 
     // make sure tmpList isn't already empty
     while (newList.Count < number && tmpList.Count > 0)
     {
         int index = Random.Range(0, tmpList.Count);
         newList.Add(tmpList[index]);
         tmpList.RemoveAt(index);
     }
 
     return newList;
 }

And this is how to use it :

 List<Item> randomItems = GetRandomItemsFromList<Item> (allItems, 5);

Hope this helps you figure this out.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

193 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How do I Instantiate a prefab in a randomly generated location specific to tile type (2D Procedural game) 1 Answer

How to stop list from getting shorter ? [Beginner] 1 Answer

Nodes are deleted from the list for no reason 1 Answer

Random instantiation endlessly? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges