Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Rolo552 · Feb 15, 2020 at 06:39 PM · gameobjectarrayobjectlistremove

How to remove objects from a list ?

Hello, I have a small problem.


I am trying to delete obejcts from a list.


What I am doing is creating a hexagonal grid, each hexagonal grid gets added to the list.After the list is full,Random objects from the list are chosen and destroyed , whoever when object gets destroyed its place in the list get replaced with -> Missing(Gameobject), My question is how could I remove this reference to the object so that only existing objects remain in the list ?


Here is my code.

 public class MapGenerator : MonoBehaviour
 {
     public int mapSize;
 
     public GameObject landType_WATER;
     public GameObject landType_LAND;
 
     public GameObject hexagonCellPrefab;
     public List<GameObject> hexagonCells = new List<GameObject>();
     public List<GameObject> land = new List<GameObject>();
 
 
     public int numberOfIslands;
 
     public bool worldIsGenerated;
     int i;
 
     Vector3 hexagonPosition;
     void Start()
     {
         GenerateMap();
     }
 
     void GenerateMap() 
     {
  
         for (int x = 0,i = 0; x < mapSize; x++)
         {
             for (int z = 0; z < mapSize; z++)
             {
                 HexagonCell hexCellInfo = landType_WATER.GetComponent<HexagonCell>();
                 
                 CreateHexagonCell(x, z, i++,hexCellInfo);
                 worldIsGenerated = true;
   
             }
         }
 
         if (worldIsGenerated) 
         {
             CreateWater();
         }
     }
     void CreateWater() 
     {
         //Choose random number from a list delete the gameobject,empty space will be used to simulate water.Remaining objects will be used as land.
         
         for (int o = 0; o < numberOfIslands; o++)
         {
             //Vygeneruj náhodné číslo mezi 0 a maximálním počtem hexagonů a vymaž je ze zásobníku
             int randomNumber = Random.Range(o, (mapSize*mapSize));
 
             var randomObject = new GameObject[numberOfIslands];
             randomObject[o] = hexagonCells[randomNumber];
 
             hexagonCells[randomNumber] = hexagonCells[o];
             hexagonCells[o] = randomObject[o];
 
             GameObject hexagonCell_Land = Instantiate<GameObject>(landType_LAND) as GameObject;
             land.Add(hexagonCells[o]);
 
             Destroy(hexagonCells[o]);
 
             //hexagonCells.RemoveAll(GameObject => GameObject == null);
             hexagonCells.RemoveAll(null);
 
         }
     }
 
     void CreateHexagonCell(int x,int z,int i,HexagonCell hexCellInfo) 
     {
         newHexagonPosition(x, z, i, hexCellInfo);
 
         //Create cell and asign under map generator parent
         GameObject hexCell = Instantiate<GameObject>(hexagonCellPrefab) as GameObject;
 
         hexagonCells.Add(hexCell);
 
         hexCell.transform.SetParent(transform, false);
         hexCell.transform.localPosition = hexagonPosition;
     }
 
     void newHexagonPosition(int x,int z,int i,HexagonCell hexCellInfo) 
     {   
         hexagonPosition.x = (x + z * 0.5f - z / 2) * (hexCellInfo.innerHexagonRadius * 2f);
         hexagonPosition.y = 0;
         hexagonPosition.z = z * (hexCellInfo.outerHexagonRadius * 1.5f);
     }    
 }
 


Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by JPhilipp · Feb 15, 2020 at 07:31 PM

Have you tried using the List functions Remove(list item) or RemoveAt(item index)?

Comment
Add comment · Show 2 · 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 Rolo552 · Feb 15, 2020 at 07:55 PM 0
Share

I have tred using that, but ins$$anonymous$$d of removing the object from the list it will only destroy and remove only 1 object and the rest doesnˇt get destroyed or removed. I even tried reasigning objects into different list but the result is the same

avatar image sacredgeometry Rolo552 · Feb 15, 2020 at 08:10 PM 1
Share

You have to pass in the index or a reference to the object you want to remove from the list. It wont "destroy" unless there are no more references to it and if its a GameObject thats in your scene there will be a reference to it until its destroyed.

avatar image
0

Answer by TjazS · Jan 02, 2021 at 01:30 PM

I was looking for an answer as well and came across this https://answers.unity.com/questions/589066/removing-from-a-list.html here is one of my scripts with a working deleting list. Also ik this post is old but probably in future mit might help some people. Sorry for the wierd code layout. Im still learning with this forums.

public List heliRockets = new List(); int rocket; int rocketsLeft; void Start() { rocketsLeft = heliRockets.Count; }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse1) && rocketsLeft > 0)
     {
         rocketsLeft--;
         rocket = Random.Range(0, rocketsLeft);
         GameObject currentRocket = heliRockets[rocket];
         heliRockets.Remove(currentRocket);
         print(currentRocket);
     }
 }
Comment
Add comment · 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
0

Answer by ugotstoopt · Feb 21, 2021 at 11:31 PM

Use a coroutine / IENumerator to clear your list, instead of as part of the Update function.

For example, instead of where you have hexagonCells.RemoveAll(null);, you could replace with StartCoroutine(RemoveCells());


Then have a separate IEnumerator that looks something like this:

 public IEnumerator RemoveCells()
     {
         yield return 0;
 
        hexagonCells.RemoveAll(item => item == null);
 
     }


I hope this works for you. Something like this worked for me when I came across a similar problem.

The reason why is that the Destroy(GameObject) function doesn't truly destroy the object until the end of the current Update frame. So the 'yield return 0;' allows that to take place and then the list can be cleared.


You can see here in the documentation where Unity's documentation mentions "Actual object destruction is always delayed until after the current Update loop, but is always done before rendering": https://docs.unity3d.com/ScriptReference/Object.Destroy.html

Comment
Add comment · 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

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

209 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Removing objects from an array 2 Answers

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

Get GameObject from List based on attached script or assigned name 3 Answers

Remove object from Array in javascript 1 Answer

Find specific prefab in a list 2 Answers


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