My attempts of what would be a nice little shortcut to scene population if I can get it to work right 1. Spawn multiple objects at random, in random locations. I have that part working, but there is a problem. PROBLEM: Sometimes multiple objects gets spawned in the same location. This is not good. Please help. Here's my code below. I think what I need is to remove spawn points from the array once an item is spawned their. Code (CSharp): public class ObjectsOnTileManager : MonoBehaviour { public Transform[] points; public GameObject[] objects; void Start(){ SpawnObjects (); } void SpawnObjects(){ for(int i = 0; i < objects.Length; i++){ int spawnIndex = Random.Range (0, points.Length); Instantiate (objects[i], points [spawnIndex].position, points [spawnIndex].rotation); } } }
Okay I solved my own issue by using a List store my points and objects. I then used: The only issue now is, I am getting ArgumentOutOfRange: Argument is out of range
I think the problem is that you're trying to remove list elements at indices greater than the size of the list. For example if there is only one element left in the list, and say the value of the element is 5, then Code (CSharp): objects.RemoveAt(spawnIndex); would try to remove the fifth element. Try this instead. Code (CSharp): objects.Remove(spawnIndex);