How to decouple inspector references

One of the things I’m seeing at the code/tutorials is that we are dragging references to objects in the Inspector practically every time.

For example, I have a reference of a Unit in the Unit Selected Visual and I have to drag the Unit first to couple them. And that is what always make me have a lot of errors and problems with the code.

So my question is: There is some way to avoid that references, make them dynamic, take them on the code itself or something like that?

I normally take them in the Awake, but I don’t think that is a good code habit. I think, I don’t really know.

This depends on many things.

When we have something in the scene that cannot easily be found using code, we would drag it into the inspector. For example, if we have multiples of a given object in the scene, but we want to assign only one of those things to a specific component, we can drag the relevant one in using the inspector. These are usually things that are not related to the component i.e. it’s not a child or on the parent branch. Perhaps we have some keys in the scene, and each key opens a specific door. Then could drag the key into a field on the door that it opens so that the door can handle it appropriately.

If it is related, we don’t have to drag it in, we can get it using GetComponent(), GetComponentInChildren() or GetComponentInParent().

If we absolutely know that there will only be one instance in the scene, we can use FindObjectOfType() in Awake() to find the instance and keep a reference to it.

2 Likes

Follup question. Is there a nice way to see what is connected to what? I can dive around the ui, but especially for code review, it would be awesome to see what a unit is connected to.

1 Like

Not natively. You may find something on the asset store. And Visual Studio shows asset references, etc., in code

This sounds nice.
I’ve seen some patterns to decouple this stuff, for example the one that you said: make the references children of a parent GO and then you just need to search into the children.

But I’ve seen more code heavy stuff for this. Though I didn’t understand a thing. For example, I saw somebody do a whole class structure withouth coupling any of the references via _variables and => methods. Idk how that works, but sounds great to make modular packages and then export them.

Thank you for the response! I will give it a thought.

For handling references there are basically 2 methods, and neither of them is perfect.

You have the method that I used in the course, making a SerializeField and dragging the reference in the Editor.
This method is prone to errors in case you just forget to assign an object or you assign the wrong object.

The other method is do it all through code which is how I used to do it since I’m primarily a programmer.
I would just have the main script and then get any references with transform.Find(“Child”);
But this method also has it’s downsides, it uses strings which are very error prone due to them being easy to mess up by capitalizing the wrong word or using an I instead of an l
If you write the wrong name you get a null ref and everything breaks.

So there really isn’t a perfect clean way, both methods have pros and cons.
I used to use the Find(); method constantly because I liked keeping as much of my game logic in my code as possible, but over the past years I’ve transitioned into using SerializeField’s because they are easier to follow for non-programmers (which is helpful when making tutorials/courses) and over time I’ve learned to enjoy that pattern.

4 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms