How to avoid having to drag and drop a lot of objects in inspector window?

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:

This is how it looks in editor/inspector.


And here is an example ingame.

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.

Thanks in advance!

It certainly is not. Have you heard about C# arrays yet?

[SerializeField] private GameObject[] currentAmountDisplay;

Then you just drag and drop all of your items into the array once.

In your code you can iterate through this array and do whatever you like to do on all of them in a for or foreach loop.

private void Start()
{
    foreach (var go in currentAmountDisplay)
    {
         Debug.Log(go.name);
    }
}

Something like this.

1 Like

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.

2 Likes

Thanks for the reply.

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.

This code is a mess. It’s a year old code of when i started Unity that i’m trying to improve.

If there’s a way to implement arrays in this case, or any other solution, i would appreciate your help.

That makes sense. Thanks to both of you for taking your time to answer me. Really appreciate.

1 Like

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
}
1 Like

You’re right, that’s exactly what i want. Would this approach be bad if used in Update() function? As it would iterate a lot in the for loop.

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

private TextMeshProUGUI display;

void Start()
{
    display = currentAmountDisplay.GetComponent<TextMeshProUGUI>();
}
1 Like