I’ve been using Unity for a while, but I’ve only been coding (for realz) for about a year. I’m currently refactoring some of the code I wrote a year ago, and seeing lots of things I can do better.
One thing I’m seeing way too much of is this:
1 2 3 4 5 6 7 |
public GameObject myButton; Image myButtonImage; void Start() { myButtonImage = myButton.GetComponent<Image>(); } |
I’m assigning a GameObject
in the inspector, and then getting the Image
component in Start()
.
What’s wrong with this?
In many cases I don’t need the GameObject
beyond the Start()
method, so having it hanging around is pretty much pointless, and getting all those components at the start (or any other time) causes an unnecessary delay.