Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheBlackBox · May 25, 2015 at 03:20 PM · c#array

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maccabbe · May 25, 2015 at 03:44 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheBlackBox · May 25, 2015 at 09:14 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Array member treated as null even though it is not. 1 Answer

Attacking more than one enemy with same tag, but unity only allows one enemy at a time? 2 Answers

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges