Search Unity

Issue displaying new materials in Unity build

Discussion in 'General Graphics' started by nkalinay, May 6, 2021.

  1. nkalinay

    nkalinay

    Joined:
    Aug 9, 2020
    Posts:
    5
    Hi folks,

    I'm at an impasse with my game and hoping someone with more knowledge might be able to help!

    I have a Shader Graph "dissolve" effect that I apply to a number of game object prefabs that are spawned n number of times during the course of my game. I'd like the dissolve effect to only apply to the individual instantiated prefabs. Initially, I was using the same material that was generated from the Shader Graph across all of my prefabs, but I soon found out that when the dissolve effect would occur, it would apply across all of the instantiated game object prefabs because it was a shared material. I got around this by generating a new material from the Shader Graph and applying it as run time to each instantiated prefabs. This works in the Unity Editor, HOWEVER, when I build the game I get a white texture for all of my prefabs when they spawn. I'm guessing the new material I create is not saved to the project, thus, Unity has no reference to display it during the build. I've read about saving the materials as an asset...but I don't know if this is practical as I will be generating n number of prefabs with new materials each game. Anyone encounter this use case and if so, how did you tackle it?

    Thanks!!!

    Here is the script I'm using to generate the new material for each instantiated prefab...setting the new material in the dissolve effect so the dissolve only applies to the newly generated materials:

    upload_2021-5-6_10-4-14.png

    This works in the Editor, but does NOT work during Build:

    upload_2021-5-6_10-6-39.png
     

    Attached Files:

  2. nkalinay

    nkalinay

    Joined:
    Aug 9, 2020
    Posts:
    5
    I figured it out...rather than creating new materials, I should have been instancing the existing materials. I guess when I was creating the materials from the shader, Unity did not have an actual reference of the material during build thus the white textures. Now that I am instancing from the existing material, Unity has a reference to display it in the build. This also probably saves on performance, although I'm not sure how much....
     
  3. phutaekwondo2001

    phutaekwondo2001

    Joined:
    May 11, 2021
    Posts:
    2
    Thank you, it's really helpful. I hope someone will explain why creating a new Material in scripts is not working in Build.