Hi guys, So i’m having a problem: i am making an inventory system but in the AddItem void i need to see if the current slot has an item and if so if it is stackable and if so if it is equal to the item we are trying to add. So i made that, the only problem is i have no idea now how to set the string to the new one with the ammount of the item we are adding in it. Like imagine i got 10 wood logs in inventory. When i try to add another 10 logs i made it so it looks if theres any other logs in inventory so it can stack them. If so then i want it to set the text in the item child to 10 + 10 = 20. Here’s the code i got:
} else if (slots [i].transform.FindChild (itemAdd.itemName).name == itemAdd.itemName) {
this.transform.FindChild("Count").GetComponent<Text>().name( + ammount);
// i want to do that in the (). Basically i want to add a number to the text. How do i make it?
Is this a Unity UI Text object? If you want to set the text of that Text UI you do this:
this.transform.FindChild("Count").GetComponent<Text>().text = amount.toString();
If your trying to change the name of the Text GameObject
this.transform.FindChild("Count").GetComponent<Text>().name = amount.toString();
Now that code will only set the string to the current amount your passing in. It seems like you wanting to save the fact you have 10 logs as a string. Then later when you want to add 10 more logs, you need to get the string 10, and add the number 10 to it, and then store the string 20.
If that is the case I think you need to step back and redesign your code. You should think about slots that look like this:
public class Slot
{
int itemID;
bool stackable;
int numberOfItem;
}
Then you can just run through your items and see if the item(or stack of items) your adding to the inventory matches the ID of anything you already have. If it doesn’t make a new slot if there is room. IF it does match, then check if its stackable, and if it is, increase numberOfItem with a simple + operation.
Then you have other code to update the graphics side of things by making the Text box display the numberOfItems.toString(); Ideally you want to separate the backend of your inventory, all the gory details of how its keeping track of what items you have and how many, from the frontend graphics as much as possible
1 Like
You’d need to parse the text to a number (either straight up Int32.Parse(yourText) or splitting it first), then add number, then convert back to string.
But storing it that way seems very clunky and error prone - you’re working with strings for quantity (and storing that in the name?). This is probably going to blow up sooner or later and is a hell to work with (as you can see right now - even a simple “add 10 to quantity” is really awkward already).
You’ll most definitely be better off having actual classes to store that info, and using components just to display it. Otherwise any operations on your inventory data will be… risky at best.
Hard to say with so little code what to change directly (unfortunately it might lead to an overhaul
), but either way if I were you I’d rethink the inventory structure immediately after encountering issues with a basic operation like this - it shouldn’t be hard, and if it is, something is way off in the structuring of data.
Edit: As always Takatok said it better 
I have put it like this but still doesnt work and gives an error:
} else if (slots [i].transform.FindChild (itemAdd.itemName).name == itemAdd.itemName) {
currentStack = this.transform.FindChild ("Count").GetComponent<Text> ().text;
currentStackInt = int.Parse (currentStack);
finalStackInt = currentStackInt + ammount;
this.transform.FindChild ("Count").GetComponent<Text> ().text = finalStackInt.ToString;
}
Whats the error. I suspect its around your int.Parse. You need to make sure that text is set to some number to start with. Like “0”. I know for a fact this will cause an error if you try to parse “Carrot” or even the empty string “”.
But again I think trying to store your inventory amounts as strings is a mistake. It might seem like a lot of extra effort now to restructure your code, but I think in the long run it will be easier to understand and maintain.