- Home /
prefab loading via resources forced to inactive
I'm having an issue with loading prefabs via Resources.Load. For some reason, the prefab is set to active=false when the prefab is instantiated. Also, the prefab instance doesn't show up in scene mode. Here's the code I'm using to instantiate the item:
public void setItemType(string name)
{
name = "Prefabs/PFX-" + name + "-" + Game.room.curThemeName;
Debug.Log(name);
pfxChild = (GameObject)Resources.Load(name);
pfxChild.active = true;
Debug.Log(pfxChild + " pos:"+pfxChild.transform.position + " active:"+pfxChild.active);
}
The final debug log from that code outputs:
"PFX-water drops-normal (UnityEngine.GameObject) pos:(393.3, 0.0, 401.2) active:False"
The prefabs seem fine - I can drag them into the scene view and they work correctly. When I instantiate through Resources.Load, the object exists as far as the script is concerned, but it doesn't appear in game or in the paused scene view. The most baffling thing though is that the object is set to active=false, even though I explicitly set it to true.
Is it somehow related to it being a particle system prefab? The prefab itself consists solely of a game object with a particle system component.
Anyone have any ideas why this is happening?
Answer by Damocles · Sep 18, 2012 at 04:02 PM
Ah, found the problem. Pretty obvious really - Resources.Load simply loads the resource for use, it doesn't instantiate it. I changed the line:
pfxChild = (GameObject)Resources.Load(name);
to:
pfxChild = (GameObject)GameObject.Instantiate((GameObject)Resources.Load(name));
and it worked as expected.
Hah. Well done. Thanks for posting back.
Now please accept your answer as the solution, so that this can be used properly by others :)
d'oh, forgot to hit the tick :) Thanks for re$$anonymous$$ding me.
Answer by The-Arc-Games · Sep 18, 2012 at 01:41 PM
It is quite probable that your problem is related to SCALE. Make sure that, in the project, the model Mesh Scale is set to 1.
FBX defaults to 0.01 scale, so it's basically invisible in respect to 'normal' objects.
There is no mesh involved here. The prefab is an empty game object with a particle system component. There is no mesh to see, only particles. Plus I have an editor script that causes all meshes to import at 0.1 scale ins$$anonymous$$d of 0.01, because I got sick of having to manually change every mesh.
Curious issue. Have you tried the same loading routine with a different prefab, made out of primitives perhaps?
Your answer
Follow this Question
Related Questions
Update a Prefab in the Resources Folder in Edit-Mode by a Script 0 Answers
how to instantiate saved objects back into game 1 Answer
How to load a prefab / object from a http source (e.g. REST API)? 2 Answers
Resource Load Prefab 0 Answers
About to gut my use of the Resources folder. What are the best practices going forward? 1 Answer