How to get a script Elegantly

plan A(when edit) public T t and then drag the target script (T) in hierarchy view
plan B single instance public static T instance instance=this and use T.instance to get
plan C static class
plan D GetComponent
have any other plans (E F G…)?
and which is the best way

Different solutions for different situations. One is not strictly better than another.

4 Likes

There’s no wrong or right way, although some approaches might be better for specific scenarios. Some people will get quite attached to a specific pattern, and you should listen to their points particularly if you are new, but ultimately use the approach that works best for you and your team.

Static instances are useful for manager classes where having a singleton makes sense. There are lots of different threads on best implementation, have a read.

GetComponent in an initialisation method is useful when you expect (or force via RequireComponent) the component to be present.

Assignment in the editor is useful when its not easy to determine from code which component should be used (for example if a complex character object had multiple animators but you only need to act on one of them).

1 Like

What is elegance?

low coupling and it makes your total code clearly

Well, that should already help you decide between those choices. Though it’s usually a bit of a trade-off.