-1

I needed to find a better way to find my Game Objects without using GameObject.Find because I have this line more than 30 time is one script, so I created this line on my script:

 public List<GameObject> gameObjectList = new List<GameObject>();

Then, I go in the inspector and add manually the gameobjects in the list. enter image description here

and everything works fine. But I see nobody talking about that way on internet, so is there something wrong with that way?

Sevlac
  • 39
  • 8
  • 1
    In general, whatever you can do to reduce the number of times you traverse the hierarchy (or minimize the hierarchy you're traversing) is best, but if you're then just traversing this list there's probably a better way; it completely depends on what the use-case is. I think that you probably don't see it often because it's a natural progression from making singular objects available in the inspector, and there are limited reasons for having that list (for example, you can use `GetComponentsInChildren` instead of storing a list of children for looking up their components). – Daevin Aug 31 '22 at 17:25
  • 30 times does sound wrong. Are you caching any results at all? – BugFinder Aug 31 '22 at 17:42
  • You mean assign stuff in adequate fields in the Inspector? ... It is **THE** way in Unity to reference your objects and components ... I doubt a lot you didn't find anyone doing this so far ^^ Little tip though: Instead of using `GameObject` rather directly use the correct component type you are interested in e.g. `Button` – derHugo Sep 01 '22 at 07:14
  • In your case you could also e.g. use `gameObjectList = GetComponentsInChildren – derHugo Sep 01 '22 at 07:16
  • What do you expect/suspect might be wrong about this? – TylerH Sep 01 '22 at 21:34

1 Answers1

1

Assigning Object references using the Inspector is a great practice! It can make your components more flexible, because you can rewire the same code to work with different Objects.

Note that you can also expose private fields using the SerializeField attribute. Making fields private when you can is good practice, because limiting access to them can help reduce bugs.

Another things to note is that you can also edit Component type fields using the Inspector, so you don't need to do any GetComponent calls in your code. You rarely need to work with GameObjects directly; basically only when you want to destroy a GameObject or modify it's active state.

[SerializeField]
private Button[] buttons = new Button[0];
Sisus
  • 651
  • 4
  • 8
  • the `= new Button[0];` is pretty unnecessary, even if you are afraid of `null` it won't happen a this field is serialized and therefore automatically initialized by Unity once the component is instantiated – derHugo Sep 01 '22 at 07:14
  • 1
    @derHugo That would be the case with scene objects and prefab instances yes, but when using GameObject.AddComponent at runtime the array would remain null unless it's initialized in code. For this reason I think it's good practice to initialize arrays in code even when it comes to serialized fields. A second reason is to avoid getting the "Field is never assigned to" warning - although this can be suppressed in Unity's settings nowadays. – Sisus Sep 01 '22 at 20:03
  • 1
    I tested and yes I was wrong and you are right, `AddComponent` seems the one exception where serialized fields are not populated! – derHugo Sep 02 '22 at 08:58