Reputation: 97
I'm currently working on a project and my next goal is to be able to instantiate a prefab during runtime, i managed to reach that far but I noticed that my instantiated prefabs doesn't have any shadows. I tried dragging the exact same object to the scene and it can cast shadow. I've did some googling and found many comments of this being a Unity3d bug in version 5, just curious whether anyone encountered the same problem and managed to find a roundabout way to fix this.
The prefabs I'm spawning are mostly all different.
The plane and cube with the shadow is dragged inside the scene whilst without shadow are instantiated.
Thanks alot for taking the time to read!
EDIT 1 So I've tried a couple of testing and trials and errors, and I noticed that the only difference between the game objects that have and doesn't have the shadow is that the gameobjects without the shadow was from an assetbundle loaded.
My problem seems to be similar to this guy's post
Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
EDIT 2
I'm using the Unity version 5.5.1f1 if that helps
Upvotes: 2
Views: 1463
Reputation: 97
Seems like instantiating through assetbundles did something to it's shader, I used this code to re-set all of the shaders in the scene and it worked for me.
Shader standardShader;
void Start() {
standardShader = Shader.Find("Standard");
}
void changeShader() // because shadow for assetbundle is cucked.
{
var renderers = FindObjectsOfType<Renderer>() as Renderer[];
for (int i = 0; i < renderers.Length; i++)
renderers[i].material.shader = standardShader;
}
and called it after all of the instantiating is done.
Upvotes: 1
Reputation: 859
Go to your prefab and check the mesh renderer
, it must have the receive shadows
and cast shadows
property activated like in the image:
Then remember to update your prefab:
Drag one prefab
to the scene, activate the receive shadows
and cast shadows
properties from the mesh renderer
and finally apply
changes.
Upvotes: 1