Why couldn't I use the “Resources.Load” result directly?
The Unity Manual says the Resources.Load returns the requested asset as an Object.I wonder why could't I use the returned Object directly.For example,I have a Text prefab and I want to add it's instance to the Hierarchy,but the Code below won't work
Text prefab;
private void Start()
{
prefab = Resources.Load<Text>("Prefabs/Text");
GameObject canvas = GameObject.Find("Canvas");
prefab.transform.SetParent(canvas.transform);
}
I must Instantiate the return of the Resources.Load first like below
Text prefab;
private void Start()
{
prefab = Resources.Load<Text>("Prefabs/Text");
GameObject canvas = GameObject.Find("Canvas");
Text text = Instantiate(prefab);
text.transform.SetParent(canvas.transform);
}
I don't know what's the difference between the Instantiate result and Resources.Load result,and what the Instantiate do ,so that it's return can be added to Hierarchy.
Answer by streeetwalker · Sep 06, 2020 at 03:05 PM
Resources.Load pulls an asset into memory, but not into the game hierarchy. In the same way that a prefab is not placed into the hierarchy when you link it to a field in an inspector. You still have to instantiate the prefab.
Objects that you author directly into the hierarchy are Instantiated at the time your game starts up. So they are included in the build and instantiated.
Assets that are linked to objects that are in the hierarchy are included in the build, but not instantiated.
All other assets that are not linked to some object in the hierarchy are not included in the build.
Resources are a way of loading assets that are not included in your build. Note that as a general rule you should not use the Resources folder for a finished game.
Your answer
Follow this Question
Related Questions
Unity won't Instantiate PLEASE HELP! 1 Answer
How do I make the same 2d object to fall again and again? 0 Answers
Polybrush won't save to prefab 1 Answer
Editor script: Variable created on gameObject in editor set to missing on runtime 0 Answers
what does the BUILD_ADDITIONAL-folder stands for ? 0 Answers