[SOLVED]Problem with material created by code

I’m using the following method to create a material with random colors at runtime:

 Material CreateRandomMaterial()
    {
      
        byte cor1 = (byte)Random.Range(0, 255);
        byte cor2 = (byte)Random.Range(0, 255);
        byte cor3 = (byte)Random.Range(0, 255);
        //byte alpha = (byte)Random.Range(0, 255);
      
        Color32 color32 = new Color32(cor1, cor2, cor3, 100); //Menos alpha, mais transparencia
        Shader shad = Shader.Find("Transparent/Diffuse");
        Material mat = new Material(shad);
        mat.shader = shad;
        mat.color = color32;

        return mat;
    }

This works perfectly if i build for windows, but if I build for the webplayer (wich is the intended target) it does not work.

Why create a material this way you ask!? Because I’m creating an infinite number of procedurally generated objects, and want to assing a different material to each of them, wich I want to be able to modify as I want later, without interfering with the materials of the other objects

Edit:
Aparently I was working on an older version and building a new one. My apologies for the confusion.
The problem is: Whenever I build the application and then run it, it no longer applies the new material to the objects. But while I’m in ‘Play’ mode, it works just fine.

bumping

“it does not work” is not a sufficient explanation of the problem :slight_smile:

Sorry, but I’m at a loss for words here. When I ‘Play’ the application, the script generates the material properly, and the object it aplies the material to, changes color. When I compile it for Windows, it also works as intended. But when I compile it to the webplayer, it no longer applies it. The object keeps the default color

Edit:
Aparently I was working on an older version and building a new one. My apologies for the confusion.
The problem is: Whenever I build the application and then run it, it no longer applies the new material to the objects. But while I’m in ‘Play’ mode, it works just fine.

So it works in the Editor but does not work in the built client? Have you checked the log for errors? How are you applying the resulting material to the object? Have you tried doing a development build and attaching the debugger to the client’s process?

Yes I’ve tried to debbug the webplayer but was unsuccessfull. Couldn’t get it to work.
I’ve made a post (on my personal account) here on the forums, trying to get some help on debugging it, but got no responses.
http://forum.unity3d.com/threads/how-to-debbug-a-web-player-build.402502/

I’m applying the material with the following code:

glass.GetComponent<MeshRenderer>().material = CreateRandomMaterial();

While in editor mode, it applies a glass-like material to my objects. But after I compile it, doesn’t anymore.

When enabling debug in the build settings are there any errors in the log? What is the result in the game in terms of the object getting the new material?

Good news! I may not have been able to debug the application but I managed to output the error anyway using the UI Canvas Text object.

The attached images show the error it is giving and the result it should give instead (what it is giving me in Editor mode).



Your Shader.Find() is failing. Make sure that shader is included in your build.

Also - to use the debugger you need to attach VS to the process which it doesn’t seem like you’re doing anywhere.

I thought Shader.Find("Transparent/Diffuse") was getting a system default shader :S
Why was it working on the Edit mode and not on the compiled mode?

The build process identifies shaders (and all other assets) you’re using in order to include as little as possible but it doesn’t inspect your code to find cases like this. Adding the shader to the list here: Unity - Manual: Graphics or putting it in a Resources folder will force the Editor to always include it in builds.

1 Like

You are correct. I made a custom shader and referenced it in the inspector.
Thanks.