How do I make it so I can use strings in my unity UI TMPro? I also need to know when I should use the different strings as two strings are for when crafting an object fails, like is there to many objects or to few, ande one is for then I succeed to craft an object.
The three texts are "try to remove an object to craft a "+outcome // "try to add an object to craft a "+outcome // "You succeeded in crafting a "+outcome
The recepies and materials are deffined with items which has Data-packages connected to them through an Item script. Every item has their own IDs.
Tuhin PaulPosted Feb 25, 2023, 9:50 AM
Hi Lucas,
To use strings in Unity UI TMPro, you can follow these steps:
1. Add a TextMeshProUGUI component to your UI element, such as a Text or InputField object.
2. In the Inspector panel, expand the TextMeshProUGUI component and find the "Text (TextMeshProUGUI)" field.
3. In the "Text (TextMeshProUGUI)" field, you can type in your desired string directly, or you can reference a string variable.
4. To reference a string variable, you can create a public string variable in your script and assign it a value in the Inspector panel. Then, you can reference this variable in the "Text (TextMeshProUGUI)" field by using the format "{variableName}".
Here's an example of how you could use string interpolation in C# to update the craftingText variable with the crafting outcome text:
In this example, we're assuming that the Item object has a Data-package containing the crafting outcome text, which we're storing in the outcomeText variable. We then use an if-else statement to check whether the player has too many or too few objects for the crafting recipe, or whether they've successfully crafted the object. Depending on the outcome, we use string interpolation to insert the outcome text into the craftingText string. Hope this helps