I didn’t know how to properly ask this question without giving examples, so i’ll explain it better here.
Also I’m not sure if the scripting section is the right place to ask this, so feel free to redirect me to other Forums.
I’m optimizing a 2D game that has a lot of gameobjects on the screen, numbers that constantly change and are activated and deactivated with .SetActive(). The way i’m doing this is: i create some gameObjects in the script with [SerializeField] and assing them in inspector dragging one by one.
My question is: How can i do it without looking like this mess and having to drag dozens of objects:
I don’t think this is a good workflow or efficient in any way. Are there other ways to do it faster and that doesn’t take almost 50 lines of code just of [SerializeField] Something.
In addition to 100% of EVERYTHING that Lurk says above, let me add this:
Each “entry” should be a single adaptor script (not this script!) that contains public fields for all the parts related to that:
button
build value
amount
etc
Then you have a collection of those things, again accessed by array, in your current script.
That way if you are accessing one of them, you don’t have to keep saying “I am talking about #3, here is your text,” and keeping that nightmare of code in sync.
Get cozy with collections / arrays / lists… otherwise you’re going to have a really hard time making games.
Yes, i do know about arrays and even thought about it. My problem is that i need to do something different to each GO and i’m not sure how i would do that.
One of the “group” of GOs will show the amount of times the player clicked a button, this number will later on be used in a function to make some calculations, which doesn’t matter here.
Each of these said GOs will receive a different variable that constantly changes. I have no idea how i could do this in a array, if it’s even possible.
Is each GO tied to their respective number? As in, will the currentAmountDisplay3 always add the value from currentAmountA3? If so, you can still use arrays, it would look something like this
for ( int i = 0; i < currentDisplays.Length; i++) // <== your array of displays
{
currentDisplays[i].GetComponent<TextMeshProUGUI>().text = "" + currentAmount[i]; // <== your array of amounts
}
Probably. Something you want to do is to use Events, which only trigger when called. You’ll want to call these events to update the UI when the values are changed, instead of every frame.
Another thing which is bad to do every Update() is to call GetComponent() every frame, you’ll want to store these values so you can access them faster