The question is answered, right answer was accepted
javascript to c# convert
Greetings..
I have a problem..i have 6 spawnPoints and 50 prefabs to choose from id like to spawn 6 randomly chosen prefabs without getting the same object twice from the list
below is a script i found on the net that does not repeat what is spawned but its not quite what i need
1: its not in c# and im joe lost on half its intentions 2: it spawns all the objects on one spawnpoint
would one of you kind Earthlings please help me..
var bodyParts : GameObject[];
function Start()
{
GenerateBodyParts();
}
function GenerateBodyParts()
{
//make a copy of the original one
//since we will be removing them from the array when we create them
var bodyPartsCopy : GameObject[] = bodyParts;
//loop through each bodypart
for (var i = 0; i < bodyParts.Length; i++)
{
//pick a random index
var bodyPartsNum : int = Random.Range(0, bodyPartsCopy.Length-1);
//instantiate the bodypart
yield WaitForSeconds(1);
Instantiate(bodyPartsCopy[bodyPartsNum], transform.position + Vector3(0.05,0.05,-0.9), Quaternion.identity);
//now need to remove the bodypart from the array so we don't create it again
//but we need to make it a JS array first, remove it, then convert back to a builtin array to be used by unity
var bodyPartsJS : Array = bodyPartsCopy;
bodyPartsJS.RemoveAt(bodyPartsNum);
bodyPartsCopy = bodyPartsJS;
}
}
thank you
Answer by OncaLupe · Nov 14, 2015 at 09:24 PM
Rather than adjusting the script to work, I decided to make one from scratch. The script you found is creating and modifying a JS array every loop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;//Allows using Lists
public class RandomSpawnNoRepeat : MonoBehaviour
{
public GameObject[] spawnPoints;
public GameObject[] spawnPrefabs;
void Start()
{
SpawnItems();
}
void SpawnItems()
{
//Counter to prevent infinite loop
int infLoopPrevent;
//Pointer to an item in the prefab list
int spawn;
//Holds pointers to items we've spawned
List<int> spawnedItems = new List<int>();
//Cycle through all spawn points
for(int i = 0; i < spawnPoints.Length; ++i)
{
infLoopPrevent = 1000;
//Pick a random item in the prefab list until we find one that's not in the spawnedItems list
do{
spawn = Random.Range(0, spawnPrefabs.Length);
--infLoopPrevent;
}while(spawnedItems.Contains(spawn) && (infLoopPrevent > 0));
//Error out if we hit an infinite loop
if(infLoopPrevent <= 0)
{
Debug.Log("Infinite loop when trying to pick a new prefab.");
return;
}
//Add this pointer to the list so it doesn't spawn again
spawnedItems.Add(spawn);
Instantiate(spawnPrefabs[spawn], spawnPoints[i].transform.position, Quaternion.identity);
}
}
}
The script you found is also a Coroutine to delay the spawning to once a second. If you need this ability, then make the following changes to my script:
SpawnItems();
to StartCoroutine(SpawnItems());
void SpawnItems()
to IEnumerator SpawnItems()
yield return new WaitForSeconds(1f);
Add this before or after the Instantiate.
Answer by GreenTraveler · Nov 14, 2015 at 10:17 PM
Dear OncaLupe,
I can not tell you in words the joy you have given me.. you went above and beyond.. not only is that script gold in my world.. you commented each statement to help me see how it all works..I am so thankful for the time you took to help..God Bless You