- Home /
Remove item from array
Hey, I have seen various questions on this but can only really find C# scripts which to me is no help. I have a script that instantiates enemies at run time at different spawnpoints (gameobjects). What I need is when and enemy is spawned at a gameobject, I need the gameobject to be destroyed (from the array) so that multiple enemies do not spawn on top of each other. Here is what I have:
var chicken:GameObject;
var spawn:Transform[];
function Start(){
var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
Instantiate(chicken,spawnpos.position, spawnpos.rotation);
}
// In a nutshell, if enemy spawns at spawnpoint1, delete spawnpoint 1 so enemy2 cannot spawn there. Any help/advice is appreciated guys :)
Answer by phodges · Oct 28, 2012 at 03:13 PM
There are all sorts of possible ways of doing this. An approach would be to do pretty much what you are already and then after each spawn event allocate a new, shorter array and copy results back into that. The overhead for that is unappealing though, so let's do something else entirely. What this sample will do is leave the initial array intact and instead build the whole random sequence during Start.
#pragma strict
import System.Collections.Generic;
var prefab: GameObject;
var points : Transform[];
private var spawnIndex : int;
private var sequence : Transform[];
function Start () {
var totalPoints : int = points.Length;
sequence = new Transform[totalPoints];
var indices : List.<int> = new List.<int>(totalPoints);
// Stock the array with the whole set of possible points
for (var i : int = 0; i < totalPoints; ++i){
indices.Add(i);
}
// Now randomly select from this list, removing items from it and filling the sequence
var random : int;
var selectedIndex : int;
var sequenceIndex : int;
while ( totalPoints ){
// Pick a point out of the set of indices
random = Random.Range(0, totalPoints);
selectedIndex = indices[random];
sequence[sequenceIndex] = points[selectedIndex];
++sequenceIndex;
// Now we've made use of this index, prevent it from being selected again
indices.RemoveAt(random);
// Less points available, make note of that so the next random selection is valid and that we exit the loop correctly.
--totalPoints;
}
}
// Call this each time you want to spawn a new item.
function SpawnOne() {
if (spawnIndex < sequence.Length){
var at : Transform = sequence[spawnIndex];
if (null != prefab){
Instantiate(prefab, at.position, at.rotation);
}else{
Debug.LogError("No prefab to spawn");
}
++spawnIndex;
}else{
Debug.Log("Spawn points are exhausted.");
}
}
// Debug method - so you can see what is going on.
function OnGUI() {
GUILayout.BeginArea(Rect(10f,10f,120f,120f));
GUILayout.BeginVertical();
for (var i : int = 0; i<sequence.Length; ++i){
if (i == spawnIndex - 1){
GUILayout.Label(String.Format("{0}] {1} <--- last spawn", i, sequence[i].name));
}else{
GUILayout.Label(String.Format("{0}] {1}", i, sequence[i].name));
}
}
GUILayout.EndVertical();
GUILayout.EndArea();
}
Don't use the JS Array class, it's slow and obsolete. Use List ins$$anonymous$$d.
A more than reasonable point. For my own work I use C#, so it was interesting to see how the generic types are imported into JS.
Excuse me I have problem !! $$anonymous$$y unity3d say EOF import !? have problem with import when I type import System.Collections.Generic;
Answer by Eric5h5 · Oct 28, 2012 at 04:05 PM
Use a List. C# answers will help you in most cases since both languages have most of the same stuff available; just translate to Unityscript syntax. (Unless you don't know C# at all, but they're actually more similar than they are different so it doesn't take much effort.)
import System.Collections.Generic;
var chicken : GameObject;
var spawn : List.<Transform>;
function Start() {
var itemNumber = Random.Range (0, spawn.Count);
var spawnpos = spawn[itemNumber];
spawn.RemoveAt (itemNumber);
Instantiate (chicken, spawnpos.position, spawnpos.rotation);
}
Thanks for this but the project is stricty to be produced in JS, although this is solid script, I am "unfamiliar" with the API. What I wish to do is stop my enemies from spawning on top of each other, would this even fix that problem?
Being produced in JS doesn't prevent you from using List...that is in fact what you should use. The code I wrote is JS. It answers your question about removing an item from the array. You really should become familiar with the .NET API, because that's what Unity uses, regardless of language.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083006im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to delet used array item 2 Answers
Setting up an array of Items 1 Answer
Preventing Items from shifting in a list 1 Answer
Can't Assign Item In Array 1 Answer
Remove an object from an array and destroy it (C#) 2 Answers