You need a reference to “Main”.
It’s all relative to what is “Main”, and if “Main” might change.
If “Main” is a persistent GameObject in the world, ready at start, you can do a couple things. Such as:
Call Find in the ‘Start’ function. This way it’s called only at start and not every time you tap.
Have a var/field on this script that you attach the gameobject to in the inspector, and call that directly.
Note - Find is not a performance killer all in all. It has the potential to be one. This isn’t black&white stuff, it’s potentially harmful stuff. Find is slow because it has to look-up across all the GameObjects in existence and see if the name matches the name you passed in. The more objects you have, the longer this look-up takes. The more frequently you call this, the more performance hits you take.
So if say you called Find in your ‘Update’ function of several gameobjects/scripts. You’re in for some hurt.
See slow is a relative term. It’s slow in computer terms. That means it could take upwards to a millisecond. Which is fast in human terms. It’s when you then compound that slowness… 100 calls to something that costs 1 millisecond in a single frame (update), and you just wasted 1/10th of a second. This means your best framerate can only be 10 frames per second. Now it’s entered human recognizable slowness.
Another Note:
I don’t call ‘Find’ even once in all my code. Funny thing is I don’t avoid ‘Find’ because it’s slow. I avoid ‘Find’ because it promotes bad programming practices. This is another concern about why you may want to avoid it.
Let’s say every time you want to access “Main” in your code, you call ‘Find’. Let’s say there is 21… err 23… err I’ve been working on this project a while, I forgot, there’s 34 calls to ‘Find(“Main”)’ out there.
Now I go and change the name of “Main” to “Main_burp”… cause you know… I had to or something. Now I have to hunt down every place where I call ‘Find(“Main”)’ and update it.
Now, lets say I also create another GameObject in my world. And its name just happens to ALSO get named “Main_burp”… lets say that’s in a prefab as well which gets created 12 times, and I start calling ‘Find’ to get a reference to that. Oh no… now I’m getting references randomly to different GameObjects all with the same name and I don’t know which is which.
Oh and in one of those 34 places I called ‘Find(“Main_burp”)’ I had a typo… it happens. And it’s 'Find(“Maan_berp”)… now my code don’t work and I can’t figure out why! Just sitting there scratching my head.
This isn’t to say it’s completely useless though. If you avoid the global ‘Find’ and only use the Find that locates children gameobjects of a parent gameobject… and use it only in Start, or other once called methods… it’s useful. This is often the best way to get a reference to say the ‘hand’ of a model’s skeleton.