Haru The Potato
Haru The Potato

Reputation: 97

Shadows for Instantiated Game Object bug?

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.

enter image description here

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

Answers (2)

Haru The Potato
Haru The Potato

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

Merunas Grincalaitis
Merunas Grincalaitis

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: enter image description here

Then remember to update your prefab:enter image description here

Drag one prefab to the scene, activate the receive shadows and cast shadows properties from the mesh renderer and finally apply changes.

Upvotes: 1

Related Questions