- Home /
How to remove instantiated object from an array
Hey there,
I'm in the process of creating an idle-clicker game. I'm currently working on the "Gadgets" (Ancients/Artifacts). To do this I instantiate a random Gadget from an array, at a position on the screen, however. I have realised that the player may end up recieving the same Gadget twice, which isn't ideal.
using UnityEngine.UI;
using System.Collections;
public class BuyGadget : MonoBehaviour {
public GameObject[] Gadgets;
public int price;
public int GadgetPosition;
public StatHandler Player;
public LevelManager LM;
public EnemyScript Enemy;
public GameObject priceText;
public GameObject GadgetDisplay;
// Use this for initialization
void Start () {
Player = GameObject.FindGameObjectWithTag ("LevelManager").GetComponent<StatHandler> ();
LM = GameObject.FindGameObjectWithTag ("LevelManager").GetComponent<LevelManager> ();
}
// Update is called once per frame
void Update () {
Enemy = GameObject.FindGameObjectWithTag ("Enemy").GetComponent<EnemyScript> ();
priceText.GetComponent<Text> ().text = price.ToString ();
if (Player.Technology >= price) {
gameObject.GetComponent<Button> ().interactable = true;
} else {
gameObject.GetComponent <Button>().interactable = false;
}
}
public void Purchase(){
Player.Technology -= price;
GameObject Gadget = Instantiate (Gadgets [Random.Range (0, 4)], new Vector2 (GadgetDisplay.transform.GetChild (GadgetPosition).transform.position.x, GadgetDisplay.transform.GetChild (GadgetPosition).transform.position.y), Quaternion.identity) as GameObject;
GadgetPosition ++;
}
}
How would I remove the instantiated GO from the array, once it had been spawned?
If any more information is required, feel free to ask and I will provide, I feel like I have covered what I need to.
Thanks - Brandon
You can't change the size of an array. However you can make a new array that is one item smaller and assign all items except the one you want to remove. Or you can use a list, which can remove items.
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx
Answer by DoTA_KAMIKADzE · May 25, 2015 at 03:50 PM
You have various options to go about that, most obvious:
1) Keeping array.
1.1) Stupid way:
int myRnd;
while (true)
{
myRnd = Random.Range(0, 4);
if (Gadgets[myRnd] != null) break;
}
GameObject Gadget = Instantiate(Gadgets[myRnd], new Vector2(GadgetDisplay.transform.GetChild(GadgetPosition).transform.position.x, GadgetDisplay.transform.GetChild(GadgetPosition).transform.position.y), Quaternion.identity) as GameObject;
Gadgets[myRnd] = null;
Why that's stupid way? Because in the end you'll be left with 1 out of 4 possible values and you'll keep randomizing until you get it ))))
1.2) The same way but instead or nullifying one picked by random just recreate array without it and randomize instead of while loop with Random.Range(0, Gadgets.Length).
1.3) Keep track of possible value in a separate list and remove values from it while you go and randomize by Random.Range(0, yourList.Count).
2) Just use List and don't mess with array in that way, it will look ~like that:
public List<GameObject> Gadgets;
public void Purchase()
{
Player.Technology -= price;
int myRnd = Random.Range(0, Gadgets.Count);
GameObject Gadget = Instantiate(Gadgets[myRnd], new Vector2(GadgetDisplay.transform.GetChild(GadgetPosition).transform.position.x, GadgetDisplay.transform.GetChild(GadgetPosition).transform.position.y), Quaternion.identity) as GameObject;
Gadgets.RemoveAt(myRnd);
GadgetPosition ++;
}
If for some reason you can't use values above 3 but you have such objects in your List then just use Random.Range(0, Gadgets.Count - amountOfObjectsAboveNum3)
3) You can use something else than List, like Dictionary for example, and so on and so forth...well there are a lot of other ways, but what I'm trying to say is that I'd go just with a List for your case unless you have something against that.
Also you might want to add some line that will check if you have remained objects at all, like for example for case#2 it will be:
if (Gadgets.Count > 0)
{
int myRnd = Random.Range(0, Gadgets.Count);
GameObject Gadget = Instantiate(Gadgets[myRnd], new Vector2(GadgetDisplay.transform.GetChild(GadgetPosition).transform.position.x, GadgetDisplay.transform.GetChild(GadgetPosition).transform.position.y), Quaternion.identity) as GameObject;
Gadgets.RemoveAt(myRnd);
}
else
{
//I have no idea if you want to do anything here but nevertheless you can, if not then just remove whole else statement at all
}
Thanks for this, it works just how I would like it to. I decided to use the list method, I've accepted the answer, thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613200250im_/https://answers.unity.com/themes/thub/images/avi.jpg)