- Home /
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);
}
}
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)) { }
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];
}
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.
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?
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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613153551im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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