- Home /
Which is more optimal? Defining variables or calling Resources.Load?
Let's say I have an AudioSource I'm using to play the music of the level. I want it's music to change each level, so is it better to have several AudioClips defined and change the AudioSource.Clip at the Start of each level or is loading each one through Resources.Load more efficient? Seems to consume less memory to me, but is that so?
Answer by bolt5 · Aug 24, 2017 at 08:52 AM
If youre ok in dragging every AudioClips in the inspector, then its ok. If not, Resources.Load is ok too. As long as you call it once or few, but not like 20 or 50 times in a row per object. You can do a static reference, if AudioSource is null then use Resources.Load once and never lose reference bc it is static. Then access it everywhere...
Answer by Hellium · Aug 24, 2017 at 09:03 AM
Check the Unity tutorial about Resources.Load:
2.1. Best Practices for the Resources System
Don't use it.
I would suggest to use AudioClips references in the inspector, it is simpler to change and less error-prone.