Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

how to my2DtextureList.Remove(myString);

Discussion in 'Scripting' started by waskar, Apr 19, 2018.

  1. waskar

    waskar

    Joined:
    Apr 7, 2013
    Posts:
    8
    hello,

    excuse my english,

    I have a list of Textures2D, all items in the list has a twin sister with same name but with a "b" extension. as so: "(layer01, layer01b, layer02, layer02b, layer03, layer03b etc...)

    then, I m making a random to pick a winner in the list, as i do so, i want to remove the winner twin sister from the list.

    so my idea is to do so : -1 make a random and get a winner.

    -a if the winner has no "b" within its name then, concatenate the winner name+"b" within a fresh string;
    -b remove from the Texture2D list the item called like the newly created string "name+b";
    -c (else) if the winner has a "b" within its name then
    within a string, remove one character from the end of the winner s name (should be the"b")
    -d remove from the 2Dtexture list the item named as the newly created string....

    here is my code:

    Code (CSharp):
    1. void killTwin()
    2. {
    3.         string myName = thegagnant.name;//outpout from the Random() within winerFirst 2DTexture list
    4.  
    5.     if (!myName.Contains("b"))
    6.     {
    7.  
    8.     string tokillTwinB = myName+"b (UnityEngine.Texture2D)";// add a "b" to...
    9.     Debug.Log("who am I "+tokillTwinB);// so far so good...
    10.     winerFirst.Remove(tokillTwinB); //...to remove the texture2D  
    11.     }
    12.     else
    13.     {
    14.     string tokillTwinA = myName.Substring(0, myName.Length - 1);// remove the last letter "b" from string to....
    15.     Debug.Log("who am I "+tokillTwinA);//...so far so good
    16.     winerFirst.Remove(tokillTwinA);// to remove the 2D texture form list;
    17.  
    18.     }
    19. }
    but the list.Remove() is giving me an error as the predicate i m giving is a string name and should be a 2DTexture...
    i have tried every thing i could all afternoon long, so i m know begging for your advise and forgiveness as i m not a programmer etc...

    Thank you !

    Mathieu.
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    If your twin textures are next to each other in the list, keep the random index, check the name and if there is a b remove the element index -1 in the list. If there is no b delete index +1.

    If your textures are not next to each other, you have to check each elements of your list. You can also use a map instead of a list and use the name as the key.
     
    waskar likes this.
  3. waskar

    waskar

    Joined:
    Apr 7, 2013
    Posts:
    8
    i was a bit afraid that it will be the answer...
    i know nothing about maps, but as all my program is using lists i think im going to manage the textures to be next to eatch other...
    thank you tho
     
  4. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Code (CSharp):
    1. public void RemoveTextureByName(List<Texture2D> list, string name)
    2.     {
    3.         foreach (Texture2D tex in list)
    4.         {
    5.             if (tex.name == name)
    6.             {
    7.                 list.Remove(tex);
    8.                 break;
    9.             }
    10.         }
    11.     }
    12.  
    13. RemoveTextureByName(winerFirst, tokillTwinB);
    You can try this.
     
    waskar likes this.
  5. waskar

    waskar

    Joined:
    Apr 7, 2013
    Posts:
    8
    and it works like a charm!
    you make my life so much easier!
    many thanks!!!
    ps: I never figured where to mark as solution...