List Search

Hey everyone tried getting my head around this one any ideas at all?

Basically in this method -

public void addCraft(string id)
  {
  for (int i = 0; i < database.crafts[i].Count; i++)
  {
  if (database.crafts[i].craftType.ToString() == id)
  {
  Crafts crafts = database.crafts;

  test = database.crafts[i].craftName;
  
  if (Input.GetMouseButtonDown(0))
  {
  Debug.Log(test);
  text1.text = test;
  }
  }
  
  }

  }

When the id is inserted at the start of the method it searches the database for the id type and then outputs the item name in the text object. All good there. Now what I need it to do is if the method finds say 3 items that have the same type i want it to output those item names in the string? In the debug it will show the three items as seperate entities but the string will only show the last one calculated. Is there a way i can get all of the items into the same string?
Cant figure this one out ive tried using Contain and Find using msdn site examples but doesnt work, this is the closest i can get.
Thanks in advance

If you want only one string with all the item names in it then create a new sting before the for loop that is empty (string sTest = “”; ). You can add strings to strings and it will tack whatever you added onto the end. As in, sTest += craftName;. Then put that string into debug. Right now what you’re ultimately doing is repeatedly overriding the test string and putting that out into text1. Since text1.text = test is still in the for loop you’re repeatedly overriding it.

Alternately you could add the text to the text1.text string in a similar way rather than to a new string. In that case before the for loop set that to “” and then add to it.

Yes perfect that now works, now…

I need to put those items into a list so I get at the moment for example:

HammerCamp FireSmelt Pot in that format

What I would like to do is have Hammer, Camp Fire, Smelt Pot
So do i add an +" , " to each addition?
Also I then want to pull each item and have it in a text list, would I need to do another loop that checks for the " , " break?

Thanks for replying :slight_smile:

Edit : - I figured I should use .Split but can I put those items into individual strings? ie-
Item 1 = Hammer
Item 2 = Camp Fire
Item 3 Smelt Pot

Almost there I think now got the string into a string array but i cannot assign the individual strings still from the array unsure how to do that tried a loop but doesn’t work here’s my progress so far. In the inspect returns

Size 3

Element 1 Hammer
Element 2 Camp Fire
Element 3 Smelt Pot

public void addCraft(string id)
  {
  test = "";   

  for (int i = 0; i < database.crafts[i].Count; i++)
  {
  if (database.crafts[i].craftType.ToString() == id)
  {
  Crafts crafts = database.crafts[i];

  test += database.crafts[i].craftName + ",";

  if (Input.GetMouseButton(0))
  {
  Debug.Log(test);
  text1.text = test;
  items = test.Split(delimiterChars);
  }
  }
  }

  }

:rage:

Yes, you can do stuff like sTest += otherstring + ", "; and whatever. It’s just like adding integers, you can just keep piling on the + other stuff.

Adding a string to another string does not add it to an array it just adds it to the string. What, exactly, are you trying to do?

I managed to figure it out in the end thank you for your replys :slight_smile:

I created a loop which split the string up into individual ones by indexing the result tough days work lol

Thank you thank you all