- Home /
Complicated Question about Arrays and Duplicate Sorting Orders!
So I'm trying to spawn a random number, of randomly selected items from an array of prefabs. Then Place them in random positions with appropriate sorting order to their z position. It works okay, except on line 30, the random number is sometimes duplicated, this leads to two or more items with the same position and sorting order which causes weird visual for when they're spawned. Here is the full Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditorInternal;
using System.Reflection;
public class Master : MonoBehaviour
{
public List<GameObject> allPrefabs = new List<GameObject> ();
List<GameObject> spawnList = new List<GameObject> ();
List<int> number = new List<int> ();
int z;
void Start ()
{
//Take the list of Prefabs and shuffle it
for (int r=0; r<allPrefabs.Count; r++)
{
GameObject temp = allPrefabs[r];
int randomIndex = Random.Range(r, allPrefabs.Count);
allPrefabs[r] = allPrefabs[randomIndex];
allPrefabs[randomIndex] = temp;
}
//Select a random amount of items to spawn, spawn each prefab, add it to a new Array for future actions
int i = Random.Range (2,allPrefabs.Count);
for (int n=0; n<i; n++)
{
GameObject temp = allPrefabs[n];
z = Random.Range(-3,4);
Renderer renderer = temp.GetComponent <SpriteRenderer>();
renderer.sortingOrder = -z;
GameObject currentSpawn = Instantiate(temp,new Vector3(Random.Range (-2,2),-1,z) , Quaternion.identity) as GameObject;
spawnList.Add (temp);
}
}
void Update ()
{
}
}
//Need to make sure that a sortingOrder is used only once to prevent weird placement
Now the trick is, I'm limited on z space, as the rooms that these objects are spawning in are limited to the -3 and 4 z axis BUT I want to spawn as many as 20 to 30 objects So I need a big range of sorting orders that will also correlate with each items z position to each other. Is there a fix that anyone can think of?
@Dameon_'s suggestion are two of the typical solutions. The third typical solutions is to put the numbers (in order) in a generic List. As the numbers come out (selected randomly), delete the entry. A fourth is to put the numbers in an array in order, then shuffle the array. The now shuffled numbers are pulled out in order.
The problem is that there is an unset amount of spawned objects until the int i is selected from a random range between at $$anonymous$$imum of 2 and max of the total count of allPrefabs. So unless I'm misunderstanding, I can't create a list or array with the numbers to choose from. I may end up having a list of 100's of items for the int i to be selected from. (Probably won't but I may have an amount of spawns potentially around the 20 to 40 range. Each of which, need to be placed in a unique position, then each sorting Order to be established in relation to their z position to each other. I don't want a sprite to be spawned closer to the camera with a lower sorting order than a sprite that spawns further from the camera, but I do need the positions to be random. Uuuuhg - Such confoos! :-\
Answer by Dameon_ · Apr 30, 2014 at 01:53 AM
You could maintain an array with previously used z positions and compare them all to the current random number to see if any previous random numbers were the same. You just set up a loop that will run as long as the randomly generated number is the same.
Generally, setting up a loop where you don't know a concrete end is coming is bad policy, but the odds are insanely against generating the same random number enough to create a loop that repeats multiple times.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity c# indexoutofrangeexception: array index is out of range 1 Answer
Best way to keep track of objects on a 3D Grid? 2 Answers
How to debug values in jagged arrays? 0 Answers