Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Any ideas on how to make this work? Remove a missing element from list? c#

TheUninvited

Active Coder
Someone recommended me ,this code which works but only if i put it on update, and i am worried if i put the loop into the update method it will cause performance issues?

When i destroy my cube i want the element to be removed , if i try to add this code into my collision it won't work.
C#:
if (timesHit >= maxHits)
{
Destroy(gameObject);
for (int i = 0; i < myList.Count; i++)
{
if (myList[i] == null)
{
myList.RemoveAt(i);
i--;
}
}
}




To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
 

Attachments

  • catch.PNG
    catch.PNG
    10.9 KB · Views: 1
Correct me if wrong, so what you're trying to do is that when you do a certain event you want the 'element' (I'm assuming object?) to be destroyed?
 
I was working a game and had a similar function. Are you receiving any errors in the Debug log?
Instead of checking every element one by one if they are null, you can retrieve the index the object resides in before destroying it.

C#:
using System.Collections.Generic;

// Get object's index
int index = myList.IndexOf(gameObject);

// Destroy the object
Destroy(gameObject);

// Remove it from the list, with the Index obtained before destroying
myList.RemoveAt(index);

Hope that helps!
For More info ->>> .NET IndexOf
 
Last edited by a moderator:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom