First and foremost mind you I am not a native programmer, but an artist so I apologize if this question seems silly.
I wrote a class called UICollectables.cs in C# and I am trying to have the variables for 2 items appear in the new US text, let’s call them ItemA and ItemB. I got ItemA working but ItemB won’t.
Now I notice since I declare Text text; it is working for ItemA but because it is the same ‘variable’ when I catch Item A it counts to item B as well, will I have to have a separate class per item?
Well are those objects called ItemA_Dsplay and ItemB_Display already present in the scene. Because if yes they are both found and the code would work on both. Since ItemB is the second to be checked, it will show the last thing it finds in your text.
A suggestion in there, checking onUpdate GameObject.Find can become a bottleneck for your game, this operation should be used once at start, and never touch it anymore.
Now I’m not really sure what you are trying to do with the object find and all, but having one reference to a Text component will result in changing just that, so it will always show the last string you input into it.
But you don’t need a separate class per tiem, you can use one class on multiple items and set it accordingly. The way you are doing is pretty much the “hardcode” way, which limits the modularity of your scripts.
But to help you here need some more info on what you are trying to do.
I am trying to setup the UI of my project. It shows HP, MP, ITEMA, ITEMB and so on. All of these off course have their own separate variables, but the only way I am getting them to show their values is to have one class per item. If I place them in ONE class, then their values only display of one variable and not their own.
As far as the gameobject.find being on update I’ll fix that
Then yes you need isntances of the class on all items, can’t use one to rule them all, unless you hardcode a huge class containing a fixed set ot variables.
For example you have a monobehaviour class called Item, you need to place this class on each item object. So if you have like in your example ItemA and ItemB and they are two gameobjects, put the monobehaviour on both of them, then fetch the variables by object.
If instead your item classes are not derived of monobehaviour you can’t attach them on a game object, but you can still have a reference to them from an array somewhere in your UICollectibles script. I’m guessing youare doing some sort of rpg inventory UI, can’t really point you on the right direction because there are countless ways to achieve what you are doing, so I’m just throwing stuff at you in hope I can help you.
Ah okay, it’s not really an RPG, just an adventure game, I am trying to keep it simple since I am sorta new to Unity. I will have basic stats for the character and basic items, I’ll focus more on the gameplay. Thank you very much for your assistance!!