Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GhostDre · Jul 09, 2014 at 09:20 AM · c#listrandom.rangeshuffle

Random Shuffle Listing

I'm in a little bit of a pickle. I have this script of an array that shows a listing of items. Now the thing is I only want this list to have five items shown out of ten and also shuffled, so you cant have the same list every time you start a new game. I was thinking if there should be a Random.Range implemented but I dont know where. Please Help I'm still a noob at this so please explain as well and Thanks. Heres the script:

 public class RayCasting : MonoBehaviour
 {
     public float pickupDistance;
     public List<Item> items;
 
 
     #region Unity
     void Start ()
     {
         Screen.lockCursor = true;
     }
     void Update ()
     {
         RaycastHit hit;
         Ray ray = new Ray(transform.position, transform.forward);
         if (Physics.Raycast(ray, out hit, pickupDistance))
         {
             foreach(Item item in items)
             {
 
                 if(Input.GetMouseButtonDown(0)) {
                     if (item.gameObject.Equals(hit.collider.gameObject))
                 {
                     numItemsCollected++;
                     item.Collect();
                     break;
                         }
                 }
             }
         }
     }
 
     void OnGUI()
     {
 
         GUILayout.BeginArea(new Rect(130,400,100,100));
         {
             GUILayout.BeginVertical();
             {
         if (numItemsCollected < items.Count)
         {
             foreach (Item item in items)
 
                         GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "X" : " ", item.name));
         }
         else
         {
             GUILayout.Label("You Win!");
         }
             }
             GUILayout.EndVertical();
     }
         GUILayout.EndArea();
     }
     #endregion
 
     #region Private
     private int numItemsCollected;
     #endregion
 }
 
 [System.Serializable]
 public class Item
 {
     public string name;
     public GameObject gameObject;
 
     public bool Collected { get; private set; }
 
     public void Collect()
     {
         Collected = true;
         gameObject.SetActive(false);
     }
 }

Comment
Add comment · Show 1
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 Noob_Vulcan · Jul 09, 2014 at 09:42 AM 0
Share

darthtelle answered you.

Just a piece of advice , Dont use Raycast in update it is very expensive . Ins$$anonymous$$d do raycasting on if(Input.Get$$anonymous$$ouseButtonDown(0)) { }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by darthtelle · Jul 09, 2014 at 09:26 AM

I would put this, or something similar, in the place wherever you initialise your item array. This code creates a new item array made up of randomly chosen items from your already populated array. Just make sure if you don't want duplicates, you check your new array.

 Item[] itemArray = new Item[5];
 
 for(int itemIndex = 0; itemIndex < itemArray.Length; itemIndex++)
 {
  int randomItem = Random.Range(0, 10);

  // Check to see whether item already exists in itemArray
 
  itemArray[itemIndex] = alreadyPopulatedArray[randomItem];
 }
Comment
Add comment · Show 2 · 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
avatar image GhostDre · Jul 12, 2014 at 06:15 AM 0
Share

Sorry for not being on here for a while but I don't understand where this go or how this should work. Can explain a bit more please.

avatar image darthtelle · Jul 15, 2014 at 07:11 AM 0
Share

So you already have an array or some sort of container holding the Items you want to use, right? I'm assu$$anonymous$$g in your code, that's what the item list is.

When you come to want to create an array of so many of these Items, you'd randomly select them and place them inside another container for use later on. We'll put these into an array called itemArray.

So you loop through the amount of items you want and for each one you use the Random.Range function to get an integer. This integer can then be used to access your already populated list and store it in the new array container.

Does this make sense?

avatar image
0

Answer by leonardo98 · Jul 18, 2020 at 07:57 PM

 void Shuffle()
 {
     for (int i = 0; i < _gameObjects.Count - 1; ++i)
     {
         int r = Random.Range(i + 1, _gameObjects.Count);
 
         GameObject tmp = _gameObjects[i];
         _gameObjects[i] = _gameObjects[r];
         _gameObjects[r] = tmp;
     }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

List woes. (C#) 1 Answer

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to repeat randomize questions in a list? 3 Answers


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